-
Notifications
You must be signed in to change notification settings - Fork 324
Excel streaming export
The difference between the streaming export and the default export is that the streaming export adopts the producer consumer mode, which allows batch data acquisition and batch writing to excel. The default mode is sxssf, which has a very low memory consumption. In a real sense, the streaming export realizes massive data export. In addition, the streaming export supports the unique characteristics of zip compression package.
There are three steps to using streaming export:
- Export configuration
DefaultStreamExcelBuilder<ArtCrowd> streamExcelBuilder = DefaultStreamExcelBuilder
.of(ArtCrowd.class) // If exporting map type data, please use of(map.class)
.threadPool(Executors.newFixedThreadPool(10))// Thread pool, optional
.capacity(10_000)// Capacity setting, used in active division excel, optional
.start();
- Data addition
The append parameter can be a list or a single data. It is recommended to append a single data, such as bean and map
// streamExcelBuilder.append(data);
streamExcelBuilder.asyncAppend(this::getDataList);
In some cases, personalized headers or summaries may be required. These requirements may involve merging rows, columns, styles and other complex layouts. DefaultStreamExcelBuilder
itself is not able to support bean. In version 3.6.0 and later, DefaultStreamExcelBuilder
supports template appending. Complex layouts are defined in the template. The appending methods are as follows:
DefaultStreamExcelBuilder<ArtCrowd> streamExcelBuilder = DefaultStreamExcelBuilder
.of(ArtCrowd.class)
.templateHandler(FreemarkerTemplateHandler.class)// Add template data, optional, suitable for extremely personalized data export
.start();
Map<String,Object> dataMap=new HashMap<>();
dataMap.put("title","测试");
streamExcelBuilder.append("/templates/xxx.ftl",dataMap);
- Completion of build
Workbook workbook = streamExcelBuilder.build();
To maximize export performance, call the nostyle() method to disable styles completely.
Defaultstreamexcelbuilder exports in sxssf mode (low memory consumption) by default, which does not support automatic column width.
Attachment export example:
Try with resource mode cannot return workbook because it will be closed in close method
try (DefaultStreamExcelBuilder<ArtCrowd> streamExcelBuilder = DefaultStreamExcelBuilder
.of(ArtCrowd.class)
.threadPool(Executors.newFixedThreadPool(10))
.start()) {
// Multithread obtains data asynchronously and appends it to excel. Join waits for thread execution to complete
for (int i = 0; i < 100; i++) {
// Data addition
defaultExcelBuilder.asyncAppend(this::getDataList);
}
// Final build
Workbook workbook = streamExcelBuilder.build();
AttachmentExportUtil.export(workbook, "艺术生信息", response);
}
Multi file export example:
Set the capacity of Excel (10_000). If it is set, a new EXCEL will be generated when the number of Excel lines reaches the capacity
try (DefaultStreamExcelBuilder<ArtCrowd> streamExcelBuilder = DefaultStreamExcelBuilder
.of(ArtCrowd.class)
.threadPool(Executors.newFixedThreadPool(10))
.capacity(10_000)
.start()) {
......
// Final build
List<Path> paths = streamExcelBuilder.buildAsPaths();
// do something
}
Zip export example:
Set excel capacity(capacity(10_000))
try (DefaultStreamExcelBuilder<ArtCrowd> streamExcelBuilder = DefaultStreamExcelBuilder
.of(ArtCrowd.class)
.threadPool(Executors.newFixedThreadPool(10))
.capacity(10_000)
.start()) {
......
// Final build
Path zip = streamExcelBuilder.buildAsZip("test");
AttachmentExportUtil.export(zip,"finalName.zip",response);
}
Export using annotations
- @ExcelModel(includeAllField,excludeParent,workbookType,sheetName,useFieldNameAsTitle,defaultValue)
- @IgnoreColumn
- @ExcelColumn(title,order,format,groups,defaultValue)
For details of corresponding notes, please refer to notes
-
Overview
概述 -
FAQ
常见问题 -
Dependency adding
依赖添加 -
Excel/Csv import
Excel/Csv导入 - 一对多导入
-
Excel default export
默认导出 -
Excel streaming export
流式导出 -
Dynamic export
动态导出 -
Excel template build
模板构建 -
CSV export
csv导出 -
Multiple sheet import
多sheet导入 -
Multiple sheet export
多sheet导出 - 聚合列&聚合导出
-
Custom style
自定义样式 -
Multilevel header
多级表头 -
Wrap within cell
单元格内换行 -
Image export
图片导出 -
Image import
图片导入 -
Hyperlink
链接 - 读取链接
-
Template row height setting
模板行高度设置 -
Drop-down-list
下拉列表 -
Custom convert
写入自定义转化 -
Formula usage
公式使用 -
Template cell setting
单元格设置 -
Header freeze
区域冻结 - 提示
-
Style support
样式支持 - 添加水印
- 按列读取
- 单元格斜线绘制
- 设置批注
- 版本日志