官方地址:https://www.yuque.com/easyexcel/doc/easyexcel

github:https://github.com/alibaba/easyexcel

介绍:EasyExcel是一个基于Java的简单、省内存的读写Excel的开源项目。在尽可能节约内存的情况下支持读写百M的Excel。64M内存1分钟内读取75M(46W行25列)的Excel

对比POI:POI在数据量的大的情况下很容易内存溢出,而EasyExcel在大数据量的情况下对内存专门做了处理,祥见:关于EasyExcel

具体代码如下(根据具体业务进行修改):

1.Controller:

  1. /**
  2. * 导出Excel -easyExcel
  3. * @param orderQuery
  4. * @return R
  5. */
  6. @Inner(value = false)
  7. @ApiOperation(value = "导出Excel", notes = "导出Excel")
  8. @SysLog("服务统计导出")
  9. @GetMapping("/exportBizOrder")
  10. public void exportBizOrder2(HttpServletResponse response, BizOrderQuery orderQuery) throws IOException {
  11. bizOrderService.exportBizOrder2(response, orderQuery);
  12. }

2.service层具体业务代码:

  1. /**
  2. * 导出查询结果-easyExcel
  3. *
  4. * @param response
  5. * @param orderQuery
  6. */
  7. void exportBizOrder2(HttpServletResponse response, BizOrderQuery orderQuery) throws IOException;

impl:

  1. /**
  2. * easyExcel
  3. * @param response
  4. * @param orderQuery
  5. * @throws IOException
  6. */
  7. @Override
  8. public void exportBizOrder2(HttpServletResponse response, BizOrderQuery orderQuery) throws IOException {
  9. // 查询导出集合方法(Mapper)
  10. List<BizOrderViewDTO> orders = selOrder(orderQuery);
  11. if (orders != null && orders.size()>0) {
  12. for (BizOrderViewDTO data : orders) {
  13. // 服务状态
  14. if (data.getOrderStatus() != null) {
  15. String orderStatus = "";
  16. if ("1".equals(data.getOrderStatus())) {
  17. orderStatus = "待服务";
  18. }
  19. if ("2".equals(data.getOrderStatus())) {
  20. orderStatus = "服务中";
  21. }
  22. if ("3".equals(data.getOrderStatus())) {
  23. orderStatus = "服务完成";
  24. }
  25. if ("4".equals(data.getOrderStatus())) {
  26. orderStatus = "服务取消";
  27. }
  28. data.setOrderStatus(orderStatus);
  29. }
  30. // 是否需要e代驾
  31. String userEDJ = "否";
  32. if ((data.getUserEDJ() != null && Integer.parseInt(data.getUserEDJ()) > 0) || (data.getCpNumber() != null && data.getCpNumber().length() > 0)) {
  33. userEDJ = "是";
  34. }
  35. data.setUserEDJ(userEDJ);
  36. }
  37. try {
  38. // EasyExcel核心代码
  39. response.setContentType("application/vnd.ms-excel");
  40. response.setCharacterEncoding(StringPool.UTF_8);
  41. String name = StrBuilder.create()
  42. // .append(BizConstants.EXPORT_TEMPLATE_NAME) // 暂时注释,测试环境中文乱码
  43. .append(DatePattern.PURE_DATETIME_MS_FORMAT.format(new Date()))// 导出的excel文件名
  44. .append(BizConstants.EXCEL_XLSX).toString();// BizConstants.EXCEL_XLSX = ".xlsx"
  45. // 这里URLEncoder.encode可以防止中文乱码
  46. String fileName = URLEncoder.encode(name, "UTF-8");
  47. //
  48. response.setHeader("fileName",fileName);
  49. EasyExcel.write(response.getOutputStream(), BizOrderViewDTO.class)
  50. .autoCloseStream(Boolean.FALSE)
  51. .sheet(BizConstants.EXPORT_BIZORDER_NAME)// excel中sheet名称
  52. .doWrite(orders);
  53. } catch (Exception e) {
  54. // 重置response
  55. response.reset();
  56. response.setContentType("application/json");
  57. response.setCharacterEncoding(StringPool.UTF_8);
  58. Map<String, String> map = new HashMap<String, String>(2);
  59. map.put("status", "failure");
  60. map.put("message", "下载文件失败" + e.getMessage());
  61. response.getWriter().println(JSON.toJSONString(map));
  62. }
  63. }
  64. }

3.查询方法selOrder

  1. /**
  2. * 条件查询
  3. * @param orderQuery
  4. * @return
  5. */
  6. private List<BizOrderViewDTO> selOrder(BizOrderQuery orderQuery) {
  7. logger.debug("BizOrderQuery:{}", orderQuery);
  8. QueryWrapper<BizOrder> queryWrapper = new QueryWrapper<>();
  9. if (orderQuery != null) {
  10. //订单编号
  11. String orderNumber = orderQuery.getOrderNumber();
  12. if (orderNumber != null && orderNumber.length() > 0) {
  13. queryWrapper.eq("order_number", orderNumber);
  14. }
  15. //订单状态
  16. Integer orderStatus = orderQuery.getOrderStatus();
  17. if (orderStatus != null && orderStatus > 0) {
  18. queryWrapper.eq("order_status", orderStatus);
  19. }
  20. //下单人手机号
  21. String customerPhone = orderQuery.getCustomerPhone();
  22. if (customerPhone != null && customerPhone.length() > 0) {
  23. queryWrapper.eq("order_phone", customerPhone);
  24. }
  25. //下单人姓名
  26. String customerName = orderQuery.getCustomerName();
  27. if (customerName != null && customerName.length() > 0) {
  28. queryWrapper.like("order_name", customerName);
  29. }
  30. //商业保单号
  31. String policyNo = orderQuery.getPolicyNo();
  32. if (policyNo != null && policyNo.length() > 0) {
  33. queryWrapper.eq("policy_number", policyNo);
  34. }
  35. Integer edjChoice = orderQuery.getEdjChoice();
  36. if (edjChoice != null) {
  37. if (edjChoice == 1) {
  38. queryWrapper.gt("goods_id", 0);
  39. queryWrapper.isNotNull("cp_number");
  40. }
  41. if (edjChoice == 0) {
  42. queryWrapper.and(Wrapper -> Wrapper.isNull("goods_id").or().lt("goods_id", 1));
  43. queryWrapper.and(Wrapper -> Wrapper.isNull("cp_number").or().eq("cp_number", ""));
  44. }
  45. }
  46. Integer payType = orderQuery.getPayType();
  47. if (payType != null && payType > 0) {
  48. queryWrapper.eq("pay_type", payType);
  49. }
  50. //订单金额检索下限
  51. String orderAmountIndexFrom = orderQuery.getOrderAmountIndexFrom();
  52. if (orderAmountIndexFrom != null && orderAmountIndexFrom.length() > 0) {
  53. queryWrapper.ge("order_amount", BigDecimal.valueOf(Double.parseDouble(orderAmountIndexFrom)));
  54. }
  55. //订单金额检索上限
  56. String orderAmountIndexTo = orderQuery.getOrderAmountIndexTo();
  57. if (orderAmountIndexTo != null && orderAmountIndexTo.length() > 0) {
  58. queryWrapper.le("order_amount", BigDecimal.valueOf(Double.parseDouble(orderAmountIndexTo)));
  59. }
  60. //实付金额检索下限
  61. String payAmountIndexFrom = orderQuery.getPayAmountIndexFrom();
  62. if (payAmountIndexFrom != null && payAmountIndexFrom.length() > 0) {
  63. queryWrapper.ge("pay_amount", BigDecimal.valueOf(Double.parseDouble(payAmountIndexFrom)));
  64. }
  65. //实付金额检索上限
  66. String payAmountIndexTo = orderQuery.getPayAmountIndexTo();
  67. if (payAmountIndexTo != null && payAmountIndexTo.length() > 0) {
  68. queryWrapper.le("pay_amount", BigDecimal.valueOf(Double.parseDouble(payAmountIndexTo)));
  69. }
  70. //下单时间检索下限
  71. String orderTimeIndexFrom = orderQuery.getOrderTimeIndexFrom();
  72. if (orderTimeIndexFrom != null && orderTimeIndexFrom.length() > 0) {
  73. queryWrapper.ge("order_time", orderTimeIndexFrom);
  74. }
  75. //下单时间检索上限
  76. String orderTimeIndexTo = orderQuery.getOrderTimeIndexTo();
  77. if (orderTimeIndexTo != null && orderTimeIndexTo.length() > 0) {
  78. queryWrapper.le("order_time", orderTimeIndexTo);
  79. }
  80. String orderEndTimeIndexFrom = orderQuery.getEndTimeIndexFrom();
  81. if (orderEndTimeIndexFrom != null && orderEndTimeIndexFrom.length() > 0) {
  82. queryWrapper.ge("end_time", orderEndTimeIndexFrom);
  83. }
  84. String orderEndTimeIndexTo = orderQuery.getEndTimeIndexTo();
  85. if (orderEndTimeIndexTo != null && orderEndTimeIndexTo.length() > 0) {
  86. queryWrapper.le("end_time", orderEndTimeIndexTo);
  87. }
  88. Integer evaluateScoreIndexFrom = orderQuery.getEvaluateScoreIndexFrom();
  89. Integer evaluateScoreIndexTo = orderQuery.getEvaluateScoreIndexTo();
  90. if (evaluateScoreIndexFrom != null && evaluateScoreIndexFrom > 0) {
  91. queryWrapper.ge("evaluate_score", evaluateScoreIndexFrom);
  92. }
  93. if (evaluateScoreIndexTo != null && evaluateScoreIndexTo > 0) {
  94. queryWrapper.le("evaluate_score", evaluateScoreIndexTo);
  95. }
  96. if (StrUtil.isNotBlank(orderQuery.getDeptIds())) {
  97. queryWrapper.in("o.dept_id", Arrays.asList(orderQuery.getDeptIds().split(",")));
  98. }
  99. }
  100. queryWrapper.orderByDesc("id");
  101. List<BizOrderViewDTO> orderData = bizOrderMapper.queryBizOrderForExport2(queryWrapper);
  102. return Optional.ofNullable(orderData).orElse(new ArrayList<BizOrderViewDTO>());
  103. }

4.返回给前端DTO类BizOrderViewDTO

  1. @Data
  2. public class BizOrderViewDTO {
  3. /**
  4. * 订单编号
  5. */
  6. @ColumnWidth(30)
  7. @ExcelProperty(value = "订单编号", index = 0)
  8. private String orderNumber;
  9. /**
  10. * 服务状态
  11. */
  12. @ColumnWidth(30)
  13. @ExcelProperty(value = "服务状态", index = 1)
  14. private String orderStatus;
  15. /**
  16. * 手机号
  17. */
  18. @ColumnWidth(30)
  19. @ExcelProperty(value = "手机号", index = 2)
  20. private String orderPhone;
  21. /**
  22. * 用户姓名
  23. */
  24. @ColumnWidth(30)
  25. @ExcelProperty(value = "用户姓名", index = 3)
  26. private String orderName;
  27. /**
  28. * 保单号
  29. */
  30. @ColumnWidth(30)
  31. @ExcelProperty(value = "保单号", index = 4)
  32. private String policyNumber;
  33. /**
  34. * 所属机构
  35. */
  36. @ColumnWidth(50)
  37. @ExcelProperty(value = "所属机构", index = 5)
  38. private String deptName;
  39. /**
  40. * 下单时间
  41. */
  42. @ColumnWidth(30)
  43. @ExcelProperty(value = "下单时间", index = 6)
  44. private String orderTime;
  45. /**
  46. * 结束时间
  47. */
  48. @ColumnWidth(30)
  49. @ExcelProperty(value = "结束时间", index = 7)
  50. private String endTime;
  51. /**
  52. * 订单金额
  53. */
  54. @ColumnWidth(15)
  55. @ExcelProperty(value = "订单金额", index = 8)
  56. private String orderAmount;
  57. /**
  58. * 是否使用e代驾
  59. */
  60. @ColumnWidth(30)
  61. @ExcelProperty(value = "是否使用e代驾", index = 9)
  62. private String userEDJ;
  63. /**
  64. * 人民币面值
  65. */
  66. @ColumnWidth(30)
  67. @ExcelProperty(value = "人民币面值", index = 10)
  68. private String rmbFaceValueObj;
  69. @ExcelIgnore
  70. private String cpNumber;
  71. }

更多API及相关注解使用方法!