官网: https://easypoi.mydoc.io/#text_228245

    1. <!-- easypoi导入导出excel -->
    2. <dependency>
    3. <groupId>cn.afterturn</groupId>
    4. <artifactId>easypoi-base</artifactId>
    5. <version> 4.2.0</version>
    6. </dependency>
    7. <dependency>
    8. <groupId>cn.afterturn</groupId>
    9. <artifactId>easypoi-web</artifactId>
    10. <version>4.2.0</version>
    11. </dependency>
    12. <dependency>
    13. <groupId>cn.afterturn</groupId>
    14. <artifactId>easypoi-annotation</artifactId>
    15. <version>4.2.0</version>
    16. </dependency>
    1. import cn.afterturn.easypoi.excel.ExcelExportUtil;
    2. import cn.afterturn.easypoi.excel.entity.ExportParams;
    3. import org.apache.poi.ss.usermodel.Workbook;
    4. import javax.servlet.ServletOutputStream;
    5. import javax.servlet.http.HttpServletResponse;
    6. import java.io.UnsupportedEncodingException;
    7. import java.util.List;
    8. public class AbstractExcelOut implements IExcelOut {
    9. private String fileName;
    10. private HttpServletResponse response;
    11. private AbstractExcelOut() {
    12. }
    13. public AbstractExcelOut(String fileName, HttpServletResponse response) {
    14. this.fileName = fileName;
    15. this.response = response;
    16. this.responseInit();
    17. }
    18. private void responseInit() {
    19. response.setContentType("application/vnd.ms-excel;charset=utf-8");
    20. response.setCharacterEncoding("utf-8");
    21. try {
    22. response.setHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes("gb2312"), "ISO8859-1") + ".xls");
    23. } catch (UnsupportedEncodingException e) {
    24. e.printStackTrace();
    25. }
    26. }
    27. // @Override
    28. // public <T extends BaseRowModel> void excelOut(List<T> dataList, Class<T> tClass) throws IOException {
    29. // ServletOutputStream out = null;
    30. // out = response.getOutputStream();
    31. // ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLS, true);
    32. // Sheet sheet = new Sheet(1, 0, tClass);
    33. // //设置自适应宽度
    34. // sheet.setAutoWidth(Boolean.TRUE);
    35. // sheet.setSheetName(fileName);
    36. // writer.write(dataList, sheet);
    37. // writer.finish();
    38. // out.flush();
    39. // response.getOutputStream().close();
    40. // out.close();
    41. // }
    42. @Override
    43. public void excelOut(ExportParams exportParams, List<?> dataList, Class<?> tClass) throws Exception {
    44. Workbook workbook = ExcelExportUtil.exportExcel(exportParams, tClass, dataList);
    45. setExportExcelFormat(workbook);
    46. }
    47. /**
    48. * excel导出时
    49. * 返回头设置下载,并设置文件名
    50. * 注意:以下设置编码格式是为了ie,当前项目是ie下使用的。360或者谷歌可能会文件名会乱码。根据自己需要调整编码。或者不用设置这么多,直接outStream输出得了。
    51. *
    52. * @param workbook
    53. * @throws Exception
    54. */
    55. private void setExportExcelFormat(Workbook workbook) throws Exception {
    56. ServletOutputStream outStream = null;
    57. try {
    58. outStream = response.getOutputStream();
    59. workbook.write(outStream);
    60. } finally {
    61. outStream.close();
    62. }
    63. }
    64. }
    1. import cn.afterturn.easypoi.excel.entity.ExportParams;
    2. import java.util.List;
    3. public interface IExcelOut {
    4. // void responseInit() throws UnsupportedEncodingException;
    5. // <T extends BaseRowModel> void excelOut(List<T> dataList, Class<T> tClass) throws IOException;
    6. void excelOut(ExportParams exportParams, List<?> dataList, Class<?> tClass) throws Exception;
    7. }
    1. String ORDER_File_NAME = "order-excel";
    2. String ORDER_SHEET_NAME = "order-excel";
    3. String ORDER_TITLE = "采购统计表";
    4. AbstractExcelOut abstractExcelOut = new AbstractExcelOut(ORDER_File_NAME, response);
    5. abstractExcelOut.excelOut(new ExportParams(ORDER_TITLE, ORDER_SHEET_NAME), resultList, YmzcOrderStatisticalVo.class);
    1. 关于合并只有两点:
    2. 1、类的数据结构。
    3. 2、注解
    4. @ExcelCollection(name = "") 集合使用
    5. @Excel(name = "采购数量合计(支)", width = 30, orderNum = "5",needMerge = true, isStatistics = true)
    6. name:列的标题名称,width :表格的宽度, orderNum :排序(注意每个类中的排序为单独的 不同类排序不通用)
    7. needMerge 是否合并单元格, isStatistics:是否进行统计(若为true,则在表格最后自动生成最终统计)
    8. @Data
    9. @ApiModel("采购统计以及导出")
    10. public class YmzcOrderStatisticalVo implements Serializable {
    11. @ExcelCollection(name = "")
    12. @ApiModelProperty(value = "")
    13. private List<ExcelSheetHead> number;
    14. /**
    15. * 采购单位名称
    16. */
    17. @ApiModelProperty(value = "采购单位名称")
    18. @Excel(name = "采购单位", width = 30, orderNum = "3",needMerge = true)
    19. private String creator;
    20. /**
    21. * 采购详情内容
    22. */
    23. @ApiModelProperty(value = "采购疫苗详情")
    24. @ExcelCollection(name = "", orderNum = "4")
    25. private List<YmzcOrderVaccineDetailsVo> detailsList;
    26. /**
    27. * 采购单位ID
    28. */
    29. @ApiModelProperty(value = "采购单位ID")
    30. private Long creatorId;
    31. /**
    32. * 采购数量合计(支)
    33. */
    34. @ApiModelProperty(value = "采购数量合计(支)")
    35. @Excel(name = "采购数量合计(支)", width = 30, orderNum = "5",needMerge = true, isStatistics = true)
    36. private Integer statisticalNumber;
    37. /**
    38. * 采购金额合计
    39. */
    40. @ApiModelProperty(value = "采购数量合计(支)")
    41. @Excel(name = "采购金额合计(元)", width = 30, orderNum = "6",needMerge = true, isStatistics = true)
    42. private BigDecimal statisticalPrice;
    43. }

    image.png