当前位置:   article > 正文

springboot+easyexcel实现导入_springboot引入easyexcel

springboot引入easyexcel

1.所需架包

  1. <dependency>
  2. <groupId>org.apache.poi</groupId>
  3. <artifactId>poi</artifactId>
  4. <version>3.17</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.poi</groupId>
  8. <artifactId>poi-ooxml</artifactId>
  9. <version>3.17</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>com.alibaba</groupId>
  13. <artifactId>easyexcel</artifactId>
  14. <version>2.1.4</version>
  15. </dependency>

2、实体类

  1. package com.yunhe.zzedu.file;
  2. import com.alibaba.excel.annotation.ExcelProperty;
  3. import com.alibaba.excel.annotation.write.style.ColumnWidth;
  4. import com.alibaba.excel.annotation.write.style.ContentRowHeight;
  5. import lombok.Data;
  6. /**
  7. * @description:
  8. * @author: bgq
  9. * @create: 2021/8/25 11:45
  10. */
  11. @Data
  12. @ContentRowHeight(10)
  13. public class Apply {
  14. @ColumnWidth(20)
  15. @ExcelProperty(value = "姓名",index = 1)
  16. private String xm;
  17. @ColumnWidth(20)
  18. @ExcelProperty(value = "身份证号",index = 2)
  19. private String sfzh;
  20. @ColumnWidth(20)
  21. @ExcelProperty(value = "性别",index = 3)
  22. private String xb;
  23. @ColumnWidth(20)
  24. @ExcelProperty(value = "出生日期",index = 4)
  25. private String csrq;
  26. }

 3、监听

  1. package com.yunhe.zzedu.web.controller.common;
  2. import com.alibaba.excel.context.AnalysisContext;
  3. import com.alibaba.excel.event.AnalysisEventListener;
  4. import com.yunhe.zzedu.common.tool.IDTool;
  5. import com.yunhe.zzedu.file.Apply;
  6. import com.yunhe.zzedu.service.base.ApplyService;
  7. import org.springframework.context.annotation.Scope;
  8. import org.springframework.stereotype.Component;
  9. import java.util.*;
  10. /**
  11. * @description:
  12. * @author: bgq
  13. * @create: 2021/8/25 11:11
  14. */
  15. @Component
  16. @Scope("prototype")
  17. public class ApplyExcelListener extends AnalysisEventListener<Apply> {
  18. private ApplyService applyService;
  19. List<Apply> datas=new ArrayList<>();
  20. String batchId = null;
  21. private static final int BATCH_COUNT = 3000;
  22. @Override
  23. public void invoke(Apply chapter, AnalysisContext analysisContext) {
  24. //数据存储到datas,供批量处理,或后续自己业务逻辑处理。
  25. datas.add(chapter);
  26. //达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM
  27. if(datas.size() >= BATCH_COUNT){
  28. saveData();
  29. // 存储完成清理datas
  30. datas.clear();
  31. }
  32. }
  33. public void setBatchId(String batchId){
  34. this.batchId = batchId;
  35. }
  36. public ApplyExcelListener(ApplyService applyService){
  37. this.applyService = applyService;
  38. }
  39. //数据处理
  40. @Override
  41. public void doAfterAllAnalysed(AnalysisContext analysisContext) {
  42. saveData();
  43. }
  44. private void saveData() {
  45. List<Map<String,Object>> mapList =new ArrayList<>();
  46. if (datas.size() >= 1) {
  47. for (int i = 0; i < datas.size(); i++) {
  48. Map<String,Object> map = new HashMap<>();
  49. Apply apply = datas.get(i);
  50. map.put("id", IDTool.getUUID32());
  51. map.put("xm",apply.getXm());
  52. map.put("sfzh",apply.getSfzh());
  53. map.put("xb",apply.getXb());
  54. map.put("csrq",apply.getCsrq());
  55. mapList.add(map);
  56. }
  57. applyService.saveBatch(mapList);
  58. }
  59. }
  60. }

4、接口

  1. @RequestMapping(value = "/importExcel",method = RequestMethod.POST)
  2. @ResponseBody
  3. public Result importExcel(MultipartRequest request){
  4. //获得工作簿
  5. ExcelReaderBuilder read = null;
  6. try(InputStream inputStream = request.getFile("file").getInputStream()) {
  7. String batchId = ((StandardMultipartHttpServletRequest) request).getParameterMap().get("batchId")[0];
  8. //实例化实现了AnalysisEventListener接口的类
  9. ApplyExcelListener listener = new ApplyExcelListener(applyService);
  10. listener.setBatchId(batchId);
  11. //获得工作簿
  12. read = EasyExcel.read(inputStream, Apply.class, listener);
  13. //获得工作表 又两种形形式可以通过下标也可以通过名字2003Excel不支持名字
  14. ExcelReaderSheetBuilder sheet = read.sheet();
  15. //进行读操作
  16. sheet.doRead();
  17. } catch (IOException e) {
  18. e.printStackTrace();
  19. }
  20. return ResultUtil.success();
  21. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/321551
推荐阅读
相关标签
  

闽ICP备14008679号