最近在做导出Excel,针对数据是一对多形式的对比后还是Easypoi比较好用。然后给大家分享哈

1.添加maven依赖

  1. <dependency>
  2. <groupId>cn.afterturn</groupId>
  3. <artifactId>easypoi-base</artifactId>
  4. <version>3.2.0</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>cn.afterturn</groupId>
  8. <artifactId>easypoi-web</artifactId>
  9. <version>3.2.0</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>cn.afterturn</groupId>
  13. <artifactId>easypoi-annotation</artifactId>
  14. <version>3.2.0</version>
  15. </dependency>

2、导出对应的实体类(一对多中的“一”)

  1. package com.zjt.shop.modules.personnel.entity.vo;
  2. import cn.afterturn.easypoi.excel.annotation.Excel;
  3. import cn.afterturn.easypoi.excel.annotation.ExcelCollection;
  4. import com.zjt.shop.common.excel.ExcelField;
  5. import com.zjt.shop.modules.order.entity.vo.CodeVo;
  6. import io.swagger.annotations.ApiModel;
  7. import io.swagger.annotations.ApiModelProperty;
  8. import lombok.Data;
  9. import java.util.List;
  10. /**
  11. * 作者:zhuLin
  12. * 日期:2020-07-30 17:36
  13. * 备注:产品信息 - 实体类
  14. */
  15. @Data
  16. @ApiModel(value = "产品信息统计导出-实体类")
  17. public class ProductSuitTjVo {
  18. //需要合并得列添加:needMerge = true 即可
  19. @Excel(name = "产品名称",needMerge = true,width = 20)
  20. @ApiModelProperty(value = "产品名称")
  21. private String msgName;
  22. @Excel(name = "产品状态",needMerge = true,width = 20)
  23. @ApiModelProperty(value = "产品状态")
  24. private String prodState;
  25. @Excel(name = "产品经理",needMerge = true,width = 20)
  26. @ApiModelProperty(value = "产品经理")
  27. private String productManager;
  28. //子集
  29. @ExcelCollection(name = "")
  30. List<SuitTjVo> suitList;
  31. }

3、子集对象实体类(一对多中的“多”)

  1. package com.zjt.shop.modules.personnel.entity.vo;
  2. import cn.afterturn.easypoi.excel.annotation.Excel;
  3. import cn.afterturn.easypoi.excel.annotation.ExcelCollection;
  4. import com.zjt.shop.common.excel.ExcelField;
  5. import com.zjt.shop.modules.order.entity.vo.CodeVo;
  6. import io.swagger.annotations.ApiModel;
  7. import io.swagger.annotations.ApiModelProperty;
  8. import lombok.Data;
  9. import java.util.List;
  10. /**
  11. * 作者:zhuLin
  12. * 日期:2020-07-30 17:36
  13. * 备注:产品信息 - 实体类
  14. */
  15. @Data
  16. @ApiModel(value = "产品信息统计导出-实体类")
  17. public class SuitTjVo {
  18. @Excel(name = "小达人佣金",width = 10)
  19. @ApiModelProperty(value = "小达人佣金")
  20. private double talentAmtMin;
  21. @Excel(name = "中达人佣金",width = 10)
  22. @ApiModelProperty(value = "中达人佣金")
  23. private double talentAmtCin;
  24. @Excel(name = "销售价",width = 30)
  25. @ApiModelProperty(value = "销售价")
  26. private String salePrice;
  27. @Excel(name = "结算价",width = 30)
  28. @ApiModelProperty(value = "结算价")
  29. private String settlePrice;
  30. }

4、导出Excel的实现方法

  1. /**
  2. * 导出Excel数据工具类
  3. * @param response
  4. * @param workbook
  5. * @param fileName
  6. * @throws Exception
  7. */
  8. public static void setExportExcelFormat(HttpServletResponse response, Workbook workbook, String fileName) throws Exception {
  9. response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(), "ISO8859-1"));
  10. response.setHeader("Pargam", "no-cache");
  11. response.setHeader("Cache-Control", "no-cache");
  12. ServletOutputStream outStream = null;
  13. try {
  14. outStream = response.getOutputStream();
  15. workbook.write(outStream);
  16. } catch (Exception e) {
  17. e.printStackTrace();
  18. log.error("导出Excel异常:" + e.getMessage());
  19. } finally {
  20. outStream.flush();
  21. outStream.close();
  22. }
  23. }

4、实例

  1. @ApiOperation("产品信息统计-导出")
  2. @PostMapping("/productTjExport")
  3. public void productTjExport (HttpServletResponse response,@RequestBody ProductStatisticsVo productVo) throws Exception {
  4. String title = "产品信息统计";
  5. log.info(title + "导出参数:{}",productVo);
  6. List<ProductSuitTjVo> list = empInfoService.productTjExport(productVo); //mybatis 一对多数据格式
  7. ExportParams params = new ExportParams();
  8. params.setTitle(title); //设置表头
  9. params.setSheetName(title); //设置sheet名
  10. Workbook workbook = ExcelExportUtil.exportExcel(params, ProductSuitTjVo.class, list);
  11. ExportExcel.setExportExcelFormat(response, workbook, title);
  12. }

导出结果如图
image.png