Java-使用 Easyexcel 导入 Excel 数据

2023年03月02日 15:45 · 阅读(437) ·

参考

使用Easyexcel导入大批量数据

Excel 模版

POM.xml

  1. <dependencies>
  2. <dependency>
  3. <groupId>com.alibaba</groupId>
  4. <artifactId>easyexcel</artifactId>
  5. <version>2.2.6</version>
  6. </dependency>
  7. </dependencies>

Model-ExcelCostPay

  1. package com.luoma.finance.dto.dataImport;
  2. import com.alibaba.excel.annotation.ExcelProperty;
  3. import com.fasterxml.jackson.annotation.JsonFormat;
  4. import lombok.Data;
  5. import java.math.BigDecimal;
  6. import java.util.Date;
  7. /**
  8. * 历史数据导入 - Excel 模版类 - 费用支付
  9. * luoma - 2023年03月02日10:18:28
  10. */
  11. @Data
  12. public class ExcelCostPay {
  13. /**
  14. * 融资操作信息表(financing_operate_info)主键 ID
  15. */
  16. private Long operateInfoId;
  17. /**
  18. * 融资产品编号
  19. */
  20. @ExcelProperty(index = 0)
  21. private String financingProductNo;
  22. /**
  23. * 费用类型名称
  24. */
  25. @ExcelProperty(index = 1)
  26. private String costTypeName;
  27. /**
  28. * 费用类型代码
  29. */
  30. @ExcelProperty(index = 2)
  31. private String costTypeCode;
  32. /**
  33. * 费用类型是否分摊,Y:是,N:否
  34. */
  35. @ExcelProperty(index = 3)
  36. private String costTypeIsShare;
  37. /**
  38. * 上期还款日,默认上期付款支付日期,如果属于第一期则手工输入或放空
  39. */
  40. @ExcelProperty(index = 4)
  41. @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
  42. private Date lastTimeRepaymentDate;
  43. /**
  44. * 还款周期
  45. */
  46. @ExcelProperty(index = 5)
  47. private String repaymentCycle;
  48. /**
  49. * 支付日期,根据上面“还款周期”字段和“上期还款日”计算得出,按“还款周期”的列表值,分别:“上期还款”+1个月、3个月、6个月、12个月、0个月
  50. */
  51. @ExcelProperty(index = 6)
  52. @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
  53. private Date payDate;
  54. /**
  55. * 费用覆盖开始日期
  56. */
  57. @ExcelProperty(index = 7)
  58. @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
  59. private Date costCoverBeginDate;
  60. /**
  61. * 费用覆盖结束日期
  62. */
  63. @ExcelProperty(index = 8)
  64. @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
  65. private Date costCoverEndDate;
  66. /**
  67. * 费用利率,保留七位小数
  68. */
  69. @ExcelProperty(index = 9)
  70. @JsonFormat(shape = JsonFormat.Shape.STRING)
  71. private BigDecimal costRate;
  72. /**
  73. * 付款实际天数=支付日期-上期还款日
  74. */
  75. @ExcelProperty(index = 10)
  76. private Integer payActualDay;
  77. /**
  78. * 本金,来源提款表剩余本金字段,保留两位小数
  79. */
  80. @ExcelProperty(index = 11)
  81. @JsonFormat(shape = JsonFormat.Shape.STRING)
  82. private BigDecimal principalAmount;
  83. /**
  84. * 实际费用=本金*费用利率*付款实际天数/(提款表)的计息用天数,保留两位小数
  85. */
  86. @ExcelProperty(index = 12)
  87. @JsonFormat(shape = JsonFormat.Shape.STRING)
  88. private BigDecimal actualCostAmount;
  89. /**
  90. * 支付状态:未申请支付/付款申请中/已支付,默认“未申请支付”
  91. */
  92. @ExcelProperty(index = 13)
  93. private String payStatus;
  94. }

Excel 读取类-CostPayListener

  1. package com.luoma.finance.listener;
  2. import com.alibaba.excel.context.AnalysisContext;
  3. import com.alibaba.excel.event.AnalysisEventListener;
  4. import com.luoma.finance.dto.dataImport.ExcelCostPay;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. /**
  8. * 类别 - 描述
  9. * luoma - 2023年03月02日11:03:33
  10. */
  11. public class CostPayListener extends AnalysisEventListener<ExcelCostPay> {
  12. /**
  13. * 存储读取 Excel 数据
  14. */
  15. private List<ExcelCostPay> listData = new ArrayList<>();
  16. public CostPayListener() {
  17. }
  18. /**
  19. * 读取 Excel 内容
  20. *
  21. * @param excelCostPay 历史数据导入 - Excel 模版类 - 费用支付
  22. * @param analysisContext 读取操作上下文
  23. */
  24. @Override
  25. public void invoke(ExcelCostPay excelCostPay, AnalysisContext analysisContext) {
  26. listData.add(excelCostPay);
  27. }
  28. /**
  29. * 读取后完成的操作
  30. *
  31. * @param analysisContext 读取操作上下文
  32. */
  33. @Override
  34. public void doAfterAllAnalysed(AnalysisContext analysisContext) {
  35. }
  36. /**
  37. * 获取读取的 Excel 数据
  38. *
  39. * @return 费用支付数据
  40. */
  41. public List<ExcelCostPay> getListData() {
  42. return listData;
  43. }
  44. }

控制器

  1. /**
  2. * 控制器 - 历史数据导入
  3. * luoma - 2023年3月1日17:11:55
  4. */
  5. @Slf4j
  6. @RestController
  7. @RequestMapping("/api/dataImport")
  8. public class DataImportController {
  9. //------------------------------------------------region 公共方法
  10. /**
  11. * 写日志
  12. *
  13. * @param logInfo 日志前缀
  14. * @param msg 日志信息
  15. * @param listLog 需要添加日志信息的 list
  16. */
  17. private void writeLog(String logInfo, String msg, List<String> listLog) {
  18. listLog.add(msg);
  19. log.info(logInfo, msg);
  20. }
  21. //------------------------------------------------endregion
  22. //------------------------------------------------region 费用支付,利息支付,本金支付
  23. /**
  24. * 历史数据导入-费用支付
  25. *
  26. * @param passWord 操作密码
  27. * @param file 导入 Excel 文件
  28. * @return 操作信息
  29. */
  30. @ApiOperation("历史数据导入-费用支付")
  31. @PostMapping("/costPay")
  32. public ResponseVO<String> costPay(String passWord, MultipartFile file) {
  33. String msg;
  34. String logInfo = "costPay >> {}";
  35. List<String> listLog = new ArrayList<>();
  36. writeLog(logInfo, "开始进行数据导入", listLog);
  37. try {
  38. //------------------------必填校验
  39. if (!passWord.equals(passWordStr)) {
  40. throw new Exception("密码不正确");
  41. }
  42. if (CommonUtil.isNull(file)) {
  43. throw new Exception("没有上传 Excel 文件");
  44. }
  45. //------------------------读取 Excel
  46. msg = "开始读取 Excel:" + file.getOriginalFilename();
  47. writeLog(logInfo, msg, listLog);
  48. CostPayListener costPayListener = new CostPayListener();
  49. EasyExcel.read(file.getInputStream(), ExcelCostPay.class, costPayListener).sheet().doRead();
  50. List<ExcelCostPay> listExcelCostPay = costPayListener.getListData();
  51. if (CommonUtil.isNull(listExcelCostPay)) {
  52. throw new Exception("Excel 文件数据为空");
  53. }
  54. msg = "读取到数据:" + listExcelCostPay.size();
  55. writeLog(logInfo, msg, listLog);
  56. for (ExcelCostPay item : listExcelCostPay) {
  57. System.out.println(CommUtil.convertToJson(item));
  58. }
  59. } catch (Exception e) {
  60. msg = "数据导入异常,异常信息:" + e.getMessage();
  61. listLog.add(msg);
  62. log.error(msg, e);
  63. } finally {
  64. writeLog(logInfo, "结束进行数据导入", listLog);
  65. msg = String.join("<br />", listLog);
  66. }
  67. return new ResponseVO<>(ResponseCode.OK, msg);
  68. }
  69. //------------------------------------------------endregion
  70. }

测试

控制台输出

  1. {"operateInfoId":null,"financingProductNo":"S2023021002","costTypeName":"承诺费用(Commitment Fee)","costTypeCode":"COMMITMENT","costTypeIsShare":"N","lastTimeRepaymentDate":null,"repaymentCycle":"1M","payDate":"2023-02-10","costCoverBeginDate":"2023-01-10","costCoverEndDate":"2023-02-10","costRate":"0.23563","payActualDay":30,"principalAmount":"2000","actualCostAmount":"1.23456","payStatus":"未申请支付"}
  2. {"operateInfoId":null,"financingProductNo":"S2023021002","costTypeName":"年费(Annual Fee)","costTypeCode":"ANNUAL","costTypeIsShare":"Y","lastTimeRepaymentDate":"2023-01-10","repaymentCycle":"3M","payDate":"2023-04-10","costCoverBeginDate":"2023-01-10","costCoverEndDate":"2023-04-10","costRate":"0.33563","payActualDay":90,"principalAmount":"2000","actualCostAmount":"2.23456","payStatus":"未申请支付"}
  3. {"operateInfoId":null,"financingProductNo":"S2023021305","costTypeName":"代理费(Agency Fee)","costTypeCode":"AGENCY","costTypeIsShare":"Y","lastTimeRepaymentDate":"2023-01-12","repaymentCycle":"6M","payDate":"2023-07-12","costCoverBeginDate":"2023-01-12","costCoverEndDate":"2023-07-12","costRate":"1.1111","payActualDay":180,"principalAmount":"9000","actualCostAmount":"11.1111","payStatus":"未申请支付"}
  4. {"operateInfoId":null,"financingProductNo":"S2023021305","costTypeName":"佣金费(Upfront Fee)","costTypeCode":"UPFRONT","costTypeIsShare":"Y","lastTimeRepaymentDate":"2023-01-12","repaymentCycle":"1M","payDate":"2023-02-12","costCoverBeginDate":"2023-01-12","costCoverEndDate":"2023-02-12","costRate":"2.2222","payActualDay":30,"principalAmount":"9000","actualCostAmount":"22.2222","payStatus":"未申请支付"}