springboot-文件上传xls及POI操作Excel
生活随笔
收集整理的這篇文章主要介紹了
springboot-文件上传xls及POI操作Excel
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、pom導入依賴文件
<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接收,設置接收大小
spring.http.multipart.maxFileSize=10Mb spring.http.multipart.maxRequestSize=10Mb3、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("導入成功");}return resultBean;}4、service層
//客戶批量導入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);//設置讀取轉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("導入成功");window.history.back(-1);}console.log(data);})}6、前端html
<input type="file" placeholder="請選擇文件" name="file" ref="inputer">7、測試
轉載于:https://www.cnblogs.com/AttackLion/p/9480519.html
總結
以上是生活随笔為你收集整理的springboot-文件上传xls及POI操作Excel的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【python 3.6】python读取
- 下一篇: NOVO Nordisk IT SOP