默认导出采用普通方式导出List< Bean >,适合小数据量场景,如数据量很大-10万+,建议使用DefaultStreamExcelBuilder,避免内存占用过多。

    导出需使用到如下注解

    • @ExcelTable(includeAllField,excludeParent,workbookType,sheetName,rowAccessWindowSize,useFieldNameAsTitle,defaultValue)(可选,用于全局设定,一般情况下只需要使用sheetName)
    • @ExcludeColumn(可选,用于排出不需要导出的字段)
    • @ExcelColumn(title,order,dateFormatPattern,groups,defaultValue,decimalFormat,style)对应注解详情请见:注解

    附件导出示例:

    1. @GetMapping("/default/excel/example")public void defaultBuild(HttpServletResponse response) throws Exception { List<ArtCrowd> dataList = this.getDataList(); Workbook workbook = DefaultExcelBuilder.of(ArtCrowd.class) .build(dataList); AttachmentExportUtil.export(workbook, "艺术生信息", response);}

    附件加密导出示例:

    1. @GetMapping("/default/excel/example")public void defaultBuild(HttpServletResponse response) throws Exception { List<ArtCrowd> dataList = this.getDataList(); Workbook workbook = DefaultExcelBuilder.of(ArtCrowd.class) .build(dataList); AttachmentExportUtil.encryptExport(workbook, "艺术生信息", response,"123456");}

    文件导出示例:

    1. List<ArtCrowd> dataList = this.getDataList();
    2. Workbook workbook = DefaultExcelBuilder.of(ArtCrowd.class)
    3. .build(dataList);
    4. FileExportUtil.export(workbook, new File("/User/demo.xlsx"));

    文件加密导出示例:

    1. List<ArtCrowd> dataList = this.getDataList();
    2. Workbook workbook = DefaultExcelBuilder.of(ArtCrowd.class)
    3. .build(dataList);
    4. FileExportUtil.encryptExport(workbook, new File("/User/demo.xlsx"),"123456");

    数据获取:

    1. private List<ArtCrowd> getDataList() {
    2. List<ArtCrowd> dataList = new ArrayList<>(1000);
    3. for (int i = 0; i < 1000; i++) {
    4. ArtCrowd artCrowd = new ArtCrowd();
    5. artCrowd.setName("李四");
    6. artCrowd.setAge(18);
    7. artCrowd.setGender("Woman");
    8. artCrowd.setPaintingLevel("一级证书");
    9. artCrowd.setDance(true);
    10. artCrowd.setAssessmentTime(LocalDateTime.now());
    11. artCrowd.setHobby("钓鱼");
    12. dataList.add(artCrowd);
    13. }
    14. return dataList;
    15. }
    16.  
    17. @ExcelTable(sheetName = "艺术生")
    18. public class ArtCrowd {
    19.  
    20. @ExcelColumn(order = 1, title = "姓名")
    21. private String name;
    22.  
    23. @ExcelColumn(order = 2, title = "年龄")
    24. private Integer age;
    25.  
    26. @ExcelColumn(order = 3, title = "性别")
    27. private String gender;
    28.  
    29. @ExcelColumn(order = 4,title = "绘画等级")
    30. private String paintingLevel;
    31.  
    32. @ExcelColumn(order = 5, title = "是否会跳舞"})
    33. private boolean dance;
    34.  
    35. @ExcelColumn(order = 6, title = "考核时间", dateFormatPattern = "yyyy-MM-dd HH:mm:ss")
    36. private LocalDateTime assessmentTime;
    37.  
    38. @ExcludeColumn
    39. private String hobby;
    40. }