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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

springboot-文件上传xls及POI操作Excel

發(fā)布時間:2024/4/15 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot-文件上传xls及POI操作Excel 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1、pom導(dǎo)入依賴文件

<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.9</version></dependency><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.3.1</version></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.4</version></dependency>

2、采用Multipart接收,設(shè)置接收大小

spring.http.multipart.maxFileSize=10Mb spring.http.multipart.maxRequestSize=10Mb

3、controller層,用@RequestParam接收

@RequestMapping(value = "/resultImport",method = RequestMethod.POST)@ResponseBodypublic ResultBean importResult(@RequestParam("file") MultipartFile file) throws Exception{ResultBean resultBean =new ResultBean();String fileName = file.getOriginalFilename();if(askService.batchImport(fileName,file) ==0){resultBean.setMsg("上傳文件格式不正確");resultBean.setCode(1);}else{resultBean.setMsg("導(dǎo)入成功");}return resultBean;}

4、service層

//客戶批量導(dǎo)入public Integer batchImport(String fileName, MultipartFile file) throws Exception{boolean notNull = false;Integer status = 1;List<ResultInfo> resultList = new ArrayList<>();if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {String error = "上傳文件格式不正確";status = 0;return status;}boolean isExcel2003 = true;if (fileName.matches("^.+\\.(?i)(xlsx)$")) {isExcel2003 = false;}InputStream is = file.getInputStream();Workbook wb = null;if (isExcel2003) {wb = new HSSFWorkbook(is);} else {wb = new XSSFWorkbook(is);}Sheet sheet = wb.getSheetAt(0);if(sheet!=null){notNull = true;}System.out.println(sheet.getLastRowNum());for (int r = 1; r < sheet.getLastRowNum()-1; r++) {Row row = sheet.getRow(r);if (row == null){continue;}ResultInfo resultInfo = new ResultInfo();AskUserInfo askUserInfo = new AskUserInfo();row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);//設(shè)置讀取轉(zhuǎn)String類型row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);row.getCell(5).setCellType(Cell.CELL_TYPE_STRING);row.getCell(6).setCellType(Cell.CELL_TYPE_STRING);row.getCell(7).setCellType(Cell.CELL_TYPE_STRING);String testId = row.getCell(1).getStringCellValue();String name = row.getCell(3).getStringCellValue();String record = row.getCell(4).getStringCellValue();String sex = row.getCell(5).getStringCellValue();String age = row.getCell(6).getStringCellValue();

String idCard = row.getCell(7).getStringCellValue();if(testId ==null || name ==null || sex==null || age==null){continue;}askUserInfo.setName(name);askUserInfo.setRecord(record);if(sex.equals("1")){askUserInfo.setSex(1);}else{askUserInfo.setSex(0);}askUserInfo.setIdcard(idCard);askUserInfo.setAge(Integer.parseInt(age));resultInfo.setTestId(testId);resultInfo.setCreateTime(new Date());System.out.println(r + name);AskUserInfo askUserInfo1 =askUserInfoRepository.save(askUserInfo);resultInfo.setAskUserInfo(askUserInfo1);resultInfoRepository.save(resultInfo);}return status;}

?

5、前端js提交

impData:function(){var vm = this;var inputDOM = this.$refs.inputer;var formdata = new FormData();formdata.append('file',inputDOM.files[0]);vm.$http.post('/admin/resultImport',formdata).then(function (res) {var data = res.data;if(data.code==0) {alert("導(dǎo)入成功");window.history.back(-1);}console.log(data);})}

6、前端html

<input type="file" placeholder="請選擇文件" name="file" ref="inputer">

7、測試

轉(zhuǎn)載于:https://www.cnblogs.com/AttackLion/p/9480519.html

總結(jié)

以上是生活随笔為你收集整理的springboot-文件上传xls及POI操作Excel的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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