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

    默认导出3.0.0.RC版本及其后续,底层采用的是DefaultStreamExcelBuilder.

    导出需使用到如下注解

    1. @ExcelModel(includeAllField,excludeParent,workbookType,sheetName,useFieldNameAsTitle,defaultValue)(可选,用于全局设定,一般情况下只需要使用sheetName)
    2. @IgnoreColumn(可选,用于排出不需要导出的字段)
    3. @ExcelColumn(title,order,format,groups,defaultValue,style)

    对应注解详情请见:注解

    默认导出默认计算宽度、斑马线背景色,若无需上述样式,请调用 noStyle() 方法

    附件导出示例:

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

    附件加密导出示例:

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

    文件导出示例:

    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. @ExcelModel(sheetName = "艺术生")
    17. public class ArtCrowd {
    18. @ExcelColumn(order = 0, title = "姓名")
    19. private String name;
    20. @ExcelColumn(order = 1, title = "年龄")
    21. private Integer age;
    22. @ExcelColumn(order = 2, title = "性别")
    23. private String gender;
    24. @ExcelColumn(order = 3,title = "绘画等级")
    25. private String paintingLevel;
    26. @ExcelColumn(order = 4, title = "是否会跳舞")
    27. private boolean dance;
    28. @ExcelColumn(order = 5, title = "考核时间", format = "yyyy-MM-dd HH:mm:ss")
    29. private LocalDateTime assessmentTime;
    30. @IgnoreColumn
    31. private String hobby;
    32. }