Skip to content

Excel默认导出

清沐 edited this page Jul 23, 2019 · 26 revisions

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

导出需使用到如下注解

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

对应注解详情请见:注解

附件导出示例:

@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);
}

附件加密导出示例:

@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");
}

文件导出示例:

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

文件加密导出示例:

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

数据获取:

private List<ArtCrowd> getDataList() {
    List<ArtCrowd> dataList = new ArrayList<>(1000);
    for (int i = 0; i < 1000; i++) {
        ArtCrowd artCrowd = new ArtCrowd();
         artCrowd.setName("李四");
         artCrowd.setAge(18);
         artCrowd.setGender("Woman");
         artCrowd.setPaintingLevel("一级证书");
         artCrowd.setDance(true);
         artCrowd.setAssessmentTime(LocalDateTime.now());
         artCrowd.setHobby("钓鱼");
        dataList.add(artCrowd);
    }
    return dataList;
}

@ExcelTable(sheetName = "艺术生")
public class ArtCrowd {

    @ExcelColumn(order = 1, title = "姓名")
    private String name;

    @ExcelColumn(order = 2, title = "年龄")
    private Integer age;

    @ExcelColumn(order = 3, title = "性别")
    private String gender;

    @ExcelColumn(order = 4,title = "绘画等级")
    private String paintingLevel;

    @ExcelColumn(order = 5, title = "是否会跳舞"})
    private boolean dance;

    @ExcelColumn(order = 6, title = "考核时间", dateFormatPattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime assessmentTime;

    @ExcludeColumn
    private String hobby;
}
Clone this wiki locally