Skip to content

Excel default export

清沐 edited this page Feb 18, 2020 · 3 revisions

The default export method is to export List<bean> in a common way, which is suitable for small data volume scenarios, such as large data volume - 100000 +. It is recommended to use DefaultStreamExcelBuilder to avoid excessive memory consumption.

The default is to export version 3.0.0.rc and its subsequent versions,the bottom layer is DefaultStreamExcelBuilder.

The following notes are required for export

  1. @ExcelModel(includeAllField,excludeParent,workbookType,sheetName,useFieldNameAsTitle,defaultValue)(Optional. It is used for global setting. Generally, only sheetname is needed)
  2. @IgnoreColumn(Optional to exclude fields that do not need to be exported)
  3. @ExcelColumn(title,order,format,groups,defaultValue,style)

For details of corresponding notes, please refer to notes

The default calculated width and background color of zebra crossing are exported by default. If the above styles are not needed, please call the nostyle() method

Attachment export example:

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

Attachment encryption export example:

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

Sample file export:

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

Sample file encryption export:

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

Data acquisition:

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

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

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

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

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

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

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

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

    @IgnoreColumn
    private String hobby;
}
Clone this wiki locally