日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

java的poi导入Excel文件

發(fā)布時(shí)間:2023/12/31 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java的poi导入Excel文件 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

首先看看前臺(tái)寫法:

<form action="poi/upload.do" method="post" enctype="multipart/form-data"> 選擇文件:<input type="file" name="productFile"> <input type="submit" value="提交"> </form>

因?yàn)楹笈_(tái)文件上傳用到了“MultipartHttpServletRequest”,所以需要配置一下項(xiàng)目,總共配置兩處。

1. 需要在spring-mvc,的beans中添加,下面這個(gè)

<!-- 定義文件上傳解析器 --><!-- 3、上傳文件需要有此配置,不然會(huì)報(bào)強(qiáng)轉(zhuǎn)錯(cuò)誤: java.lang.ClassCastException: org.apache.catalina.connector.RequestFacade cannot be cast to org.springframework.web.multipart.MultipartHttpServletRequest --><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><!-- 大小單位:bytes --><property name="maxUploadSize"> <value>104857600</value> </property> <property name="maxInMemorySize"> <value>10240</value> </property> <property name="defaultEncoding"> <value>UTF-8</value> </property> </bean>

2. 然后添加一個(gè)pom依賴,不是maven項(xiàng)目的可以自己去下載一個(gè)jar包。

<dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency>

后臺(tái)代碼如下:

//EXCEl上傳@RequestMapping("/upload.do")public void uploadExcel(MultipartHttpServletRequest request) throws Exception{//獲取MultipartHttpServletRequest上傳過來的文件。MultipartFile file=request.getFile("productFile");//獲取上傳的文件名稱。String fileName=file.getOriginalFilename();//根據(jù)名稱獲取文件后綴String suffix=fileName.substring(fileName.lastIndexOf("."),fileName.length());//通過文件名獲取文件格式//新建一個(gè)excel工作簿W(wǎng)orkbook wk=null;//根據(jù)文件后綴判斷excel的版本, 并將傳過來的文件塞到excel工作簿。03以下版本格式為.xsl, 07以上版本用.xlsxif(suffix.equals(".xls")) {wk=new HSSFWorkbook(file.getInputStream());}else if(suffix.equals(".xlsx")) {wk=new XSSFWorkbook(file.getInputStream());}//將excel工作簿轉(zhuǎn)換為一個(gè)list集合,泛型為Project,也就是自己要插入數(shù)據(jù)庫(kù)的對(duì)象List<Product> products=poiService.readExcelData(wk);//將list插入數(shù)據(jù)庫(kù)當(dāng)中for(Product p:products){System.out.println("待插入的數(shù)據(jù): {} "+p);int a=userDao.insertSelective(p);}}

其中需要主要的是readExcelData(wk),細(xì)節(jié)如下:

public static List<Product> readExcelData(Workbook wb) throws Exception{Product product=null;List<Product> products=new ArrayList<Product>();Row row=null;//獲取有多少sheet頁(yè)int numSheet=wb.getNumberOfSheets();if (numSheet>0) {for(int i=0;i<numSheet;i++){//getSheetAt 獲取索引Sheet sheet=wb.getSheetAt(i);int numRow=sheet.getLastRowNum();if (numRow>0) {for(int j=1;j<=numRow;j++){//TODO:跳過excel sheet表格頭部row=sheet.getRow(j);product=new Product();String name=ExcelUtil.manageCell(row.getCell(1), null);String unit=ExcelUtil.manageCell(row.getCell(2), null);Double price=Double.valueOf(ExcelUtil.manageCell(row.getCell(3), null));String stock=ExcelUtil.manageCell(row.getCell(4), null);String value=ExcelUtil.manageCell(row.getCell(5), "yyyy-MM-dd");String remark=ExcelUtil.manageCell(row.getCell(6), null);product.setName(name);product.setUnit(unit);product.setPrice(price);product.setStock(Double.valueOf(stock));System.out.println("value:"+value);product.setPurchaseDate(DateUtil.strToDate(value, "yyyy-MM-dd"));product.setRemark(remark);products.add(product);}}}}return products;}

dao層如下:

int insertSelective(Product record);

Mapper層如下:

<insert id="insertSelective" parameterType="cn.temptation.domain.Product" >insert into product<trim prefix="(" suffix=")" suffixOverrides="," ><if test="id != null" >id,</if><if test="name != null" >name,</if><if test="unit != null" >unit,</if><if test="price != null" >price,</if><if test="stock != null" >stock,</if><if test="remark != null" >remark,</if><if test="purchaseDate != null" >purchase_date,</if><if test="isDelete != null" >is_delete,</if></trim><trim prefix="values (" suffix=")" suffixOverrides="," ><if test="id != null" >#{id,jdbcType=INTEGER},</if><if test="name != null" >#{name,jdbcType=VARCHAR},</if><if test="unit != null" >#{unit,jdbcType=VARCHAR},</if><if test="price != null" >#{price,jdbcType=DOUBLE},</if><if test="stock != null" >#{stock,jdbcType=INTEGER},</if><if test="remark != null" >#{remark,jdbcType=VARCHAR},</if><if test="purchaseDate != null" >#{purchaseDate,jdbcType=DATE},</if><if test="isDelete != null" >#{isDelete,jdbcType=INTEGER},</if></trim></insert>

?

總結(jié)

以上是生活随笔為你收集整理的java的poi导入Excel文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。