读写Excel基本代码
直接复制不一定能用
实体类
@ExcelIgnore 在导出操作中不会被导出
@ExcelProperty 在导入过程中 可以根据导入模板自动匹配字段, 在导出过程中可用于设置导出的标题名字
@Getter @Setter public class Material{ @ExcelIgnore private Long id; /** 所属部门 */ @ExcelProperty(value = {"一级合并标题","所属部门"}) private String department; /** 所属部室/项目部/站区 */ @ExcelProperty(value = {"一级合并标题","所属部室/项目部/站区"}) private String area; }
监听类
监听类控制导入数据的规则限制,监听类继承AnalysisEventListener<Object>
类实现接口。
监听中无法调用Spring接口,使用反序列化注入Bean工厂中使用
public class MaterialListener extends AnalysisEventListener<Material> { // 反序列化注入bean工厂 ISysDictDataService dictDataService = SpringUtils.getBean(ISysDictDataService.class); //可以通过实例获取该值 private List<Material> datas = new ArrayList<>(); //错误信息 private List<String> errorInfo = new ArrayList<>(); @Override public void invoke(Material material, AnalysisContext analysisContext) { doSomething(material,analysisContext);//根据自己业务做处理 datas.add(material);//数据存储到list,供批量处理,或后续自己业务逻辑处理。 } private void doSomething(Material object, AnalysisContext analysisContext) { //获取当前行数 Integer row = analysisContext.readRowHolder().getRowIndex(); SysDictData sysDictData=new SysDictData(); if (ObjectUtils.isBlank(object.getType())){ errorInfo.add(" 第" + (row+1) + "行:物资种类不能为空,请重新填写后再导入!<br>"); }else { sysDictData.setDictType("material_type"); sysDictData.setDictLabel(object.getType()); List<SysDictData> list3= dictDataService.selectDictDataList(sysDictData); if (ObjectUtils.isBlank(list3)){ errorInfo.add(" 第" + (row+1) + "行:物资种类:"+object.getType()+"未在数据字典中找到,请先填写后再导入!<br>"); } } } //返回结果集对象 public List<Material> getDatas() { return datas; } public void setDatas(List<Material> datas) { this.datas = datas; } //返回错误消息 public List<String> getErrorInfo() { return errorInfo; } public void setErrorInfo(List<String> errorInfo) { this.errorInfo = errorInfo; } @Override public void doAfterAllAnalysed(AnalysisContext analysisContext) { } }
控制器
导入数据方法
在materialService.saveList(materialList)
中进行处理导入获取过来的数据,用于存取业务数据
@PostMapping("/importData") @ResponseBody public AjaxResult importData(@RequestParam MultipartFile file) throws Exception { MaterialListener excelListener = new MaterialListener(); // 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭 //EasyExcel.read(哪个文件).sheet(那张sheet表).head(表头什么样子).headRowNumber(表头占几行).registerReadListener(处理数据的监听器类).doRead() EasyExcel.read(file.getInputStream(), Material.class, excelListener).sheet().head(Material.class).headRowNumber(2).doRead(); if(ObjectUtils.isBlank(excelListener.getErrorInfo())){ if(ObjectUtils.isNotBlank(excelListener.getDatas())){ List<Material> materialList = excelListener.getDatas(); materialService.saveList(materialList); } return AjaxResult.success(1); }else{ return AjaxResult.error("操作失败",excelListener.getErrorInfo()); } }
导出数据方法
单sheet导出在注释的方法出,多sheet导出如下所示
@GetMapping("/exportData") public void exportData(Material material, HttpServletResponse response){ //换方法连表查询 // List<Material> list = materialService.selectMaterialListByDict(material); List<Material> materialList=new ArrayList<>(); List<Material> list = materialService.selectMaterialList(material); String[] ids=new String[list.size()]; for (Material p:list) { //用于修改导出的数据 String list7= dictDataService.selectDictLabel("material_type",p.getType()); if (ObjectUtils.isNotBlank(list7)){ p.setType(list7); } materialList.add(p); } List<MaterialSolid> materialSolidList=new ArrayList<>(); List<MaterialSolid> materialSolidListS = materialSolidService.selectMaterialSolidListByIds(ids); for (MaterialSolid solid:materialSolidListS) { String list7= dictDataService.selectDictLabel("material_type",solid.getType()); if (ObjectUtils.isNotBlank(list7)){ solid.setType(list7); } materialSolidList.add(solid); } try { response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); // 这里URLEncoder.encode可以防止中文乱码 当然和EASYEXCEL没有关系 String fileName = URLEncoder.encode("物料信息台账", "UTF-8"); response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx"); //调用方法进行写的操作 // EasyExcel.write(response.getOutputStream(),Material.class).sheet("物料信息台账").doWrite(materialList); //多sheet页导出 ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build(); WriteSheet writeSheet1 = EasyExcel.writerSheet(0, "物料信息总账").head(Material.class).build(); WriteSheet writeSheet2 = EasyExcel.writerSheet(1, "物料信息详情").head(MaterialSolid.class).build(); excelWriter.write(materialList, writeSheet1); excelWriter.write(materialSolidList, writeSheet2); excelWriter.finish(); } catch (Exception e) { e.printStackTrace(); } }