赞
踩
使用easypoi/eayexcel写项目时出现导入正常使用导出不能用
修改依赖后导出正常使用导入不能用
1.依赖只要必要的两个:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.51</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
2.最重要的一点,导出的接口返回void才不会报错!!!
在swagger测试导出表名会乱码,可以直接在浏览器访问地址会发现此时是不会乱码的,不会影响功能
//导出接口 public void getddfClewExcel(HttpServletResponse response, AdminDdfClewVo clewExcel) throws IOException { try{ response.setContentType("application/vnd.ms-excel;charset=utf-8"); String Excelname="这里设置一个统一的表名,也可以使用实体类的某一列做表名"; response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(Excelname,"UTF-8") + ".xlsx"); // excel头策略 WriteCellStyle headWriteCellStyle = new WriteCellStyle(); WriteFont headWriteFont = new WriteFont(); headWriteFont.setFontHeightInPoints((short) 11); headWriteFont.setBold(false); headWriteCellStyle.setWriteFont(headWriteFont); // excel内容策略 WriteCellStyle contentWriteCellStyle = new WriteCellStyle(); WriteFont contentWriteFont = new WriteFont(); contentWriteFont.setFontHeightInPoints((short) 11); contentWriteCellStyle.setWriteFont(contentWriteFont); // 设置handler HorizontalCellStyleStrategy styleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle); /** 导出数据查询方法,实现按条件查询导出,这里的DdfClewExcel也是不能随便添加字段进去的只写自己需要的数据,不然会出现导出的表中莫名多了一列0的数字的情况*/ List<DdfClewExcel> ddfClewExcels = ddfClewService.getddfClewExcel(clewExcel.getUid(),clewExcel.getId(), clewExcel.getProjectName(),clewExcel.getStartDate(),clewExcel.getEndDate(),clewExcel.getClewStatus(),clewExcel.getUserName(), clewExcel.getAddress(),clewExcel.getSource()); EasyExcel.write(response.getOutputStream(), DdfClewExcelVo.class) //这里的DdfClewExcelVo是显示的表内容,不需要的列就不要加在里面,创建一个这个类,给每个需要显示的字段加上注解 @ExcelProperty(value = "列名",index = 显示在第几列,从0开始为第一列) .sheet("下载excel服务") .registerWriteHandler(styleStrategy) .doWrite(ddfClewExcels); log.info("数据导出成功"); ResultData.success(ddfClewExcels,"数据导出成功"); }catch (Exception e){ e.printStackTrace(); ResultData.fail(ResponseCode.ERROR.val(),"数据导出失败"); }
这是实现类的代码
@Override public ResultData exportExcel(MultipartFile file) throws Exception { //读取工作簿 Workbook workBook = WorkbookFactory.create(file.getInputStream()); //读取工作表 Sheet sheet = workBook.getSheetAt(0); int rowNumber = sheet.getPhysicalNumberOfRows(); //校验是否填写内容 if (rowNumber <= 1) { return ResultData.fail(ResponseCode.ERROR.val(), "文件无内容"); } //循环读取每一行数据并校验 for (int i = 1; i < rowNumber; i++) { try { //读取行 Row row = sheet.getRow(i); //读取单元格,这是实体类,把列中的数据set进去就好了 Advised advised = new Advised(); //获取第二列数据,与实体类中的字段对应 row.getCell(1).setCellType(Cell.CELL_TYPE_STRING); advised.setCallnumber(row.getCell(1).getStringCellValue());// //获取第六列数据,与实体类中的字段对应 row.getCell(5).setCellType(Cell.CELL_TYPE_STRING); advised.setAddress(row.getCell(5).getStringCellValue());// //以此类推,不要的列就不set //============== //调用写好的新增方法,把获取指定的列数据插入到数据库 advisedMapper.addAdvised(advised); } catch (Exception e) { e.printStackTrace(); return ResultData.fail(ResponseCode.ERROR.val(), "数据导入失败"); } } return ResultData.success(ResponseCode.SUCCESS,"数据导入成功"); }
这是控制器接口写法
@PostMapping(value="/exportAdvisedExcel",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResultData exportExcel(@RequestParam MultipartFile file)throws Exception{
return advisedService.exportExcel(file);
}
参照这两段 就能实现导入导出功能了
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。