easyexcel-write
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseEasyExcel 数据写入(Write)
EasyExcel Data Writing (Write)
官方文档:https://easyexcel.opensource.alibaba.com/docs/current/quickstart/write 官方示例:https://github.com/alibaba/easyexcel/blob/master/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java 当前版本:(Apache-2.0,仓库已归档,仅做 Bug 修复)com.alibaba:easyexcel:4.0.3
本技能专注于 "程序化生成 Excel" 场景。当用户没有预制模板、要求用 Java 对象/Map 直接生成结构化 Excel(订单、用户、财务、库存、报表、对账单、审批单)时,应当使用本技能。
Official documentation: https://easyexcel.opensource.alibaba.com/docs/current/quickstart/write Official examples: https://github.com/alibaba/easyexcel/blob/master/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java Current version:(Apache-2.0; the repository has been archived and now receives bug fixes only)com.alibaba:easyexcel:4.0.3
This skill focuses on programmatically generating Excel files. Use it when there is no predefined template and structured Excel files such as orders, users, financial records, inventory, reports, statements, or approval forms need to be generated directly from Java objects or Maps.
Capability Boundaries
Capability Boundaries
✅ 强项
✅ Strengths
- 最简写入(JDK8+ 写法、传统写法、Builder 写法)
- 复杂表头(多级表头 )
@ExcelProperty({"主标题", "副标题"}) - 选择性导出列(/
includeColumnFiledNames)excludeColumnFiledNames - 注解定义格式(
@ColumnWidth@ContentRowHeight@DateTimeFormat)@NumberFormat - 自定义样式(注解方式 / 拦截器方式 / )
HorizontalCellStyleStrategy - 合并单元格(/
@ContentLoopMerge)LoopMergeStrategy - 图片导出(File/InputStream/URL/byte[] 多种来源)
- 超链接/批注/公式/富文本(体系)
WriteCellData - 动态表头(运行时决定表头)
- 多次写入/多 Sheet/多 Table
- Web 直接输出到
HttpServletResponse
- Simplest write methods (JDK8+ style, traditional style, and Builder style)
- Complex headers (multi-level headers such as )
@ExcelProperty({"主标题", "副标题"}) - Selective column export (/
includeColumnFiledNames)excludeColumnFiledNames - Annotation-based formatting (,
@ColumnWidth,@ContentRowHeight,@DateTimeFormat)@NumberFormat - Custom styles (annotation-based, interceptor-based, and )
HorizontalCellStyleStrategy - Merged cells (/
@ContentLoopMerge)LoopMergeStrategy - Image export from multiple sources (File/InputStream/URL/byte[])
- Hyperlinks, comments, formulas, and rich text (the system)
WriteCellData - Dynamic headers (determined at runtime)
- Multiple writes, multiple Sheets, and multiple Tables
- Direct web output to
HttpServletResponse
⚠️ 限制
⚠️ Limitations
- 单 Sheet 建议 < 5000 行一次性写入(数据量大用分批)
- 样式对象最多创建 6W 个,否则 Excel 打不开
- 图片会驻内存,超大图建议上传到 OSS
- 批注需要
inMemory(true) - 03 版()兼容性需用
.xlsexcelType(ExcelTypeEnum.XLS)
- It is recommended to write fewer than 5,000 rows at once per Sheet; use batches for large datasets
- A maximum of 60,000 style objects can be created; otherwise, the Excel file cannot be opened
- Images remain in memory; upload oversized images to OSS
- Comments require
inMemory(true) - For 03-version () compatibility, use
.xlsexcelType(ExcelTypeEnum.XLS)
❌ Out of Scope(不该用本技能的场景,请改用其它技能)
❌ Out of Scope (scenarios where this skill should not be used; use another skill instead)
- 按预制模板填充 → 不适用本技能,使用
easyexcel-fill - 读取 Excel 数据 → 不适用本技能,使用
easyexcel-read - CSV 文件处理 → 不适用本技能,自行使用 commons-csv / OpenCSV
- Word/PPT → 不适用本技能,使用 Apache POI / Apache POI-XML
- Filling predefined templates → Not applicable to this skill; use
easyexcel-fill - Reading Excel data → Not applicable to this skill; use
easyexcel-read - CSV file processing → Not applicable to this skill; use commons-csv / OpenCSV directly
- Word/PPT → Not applicable to this skill; use Apache POI / Apache POI-XML
Data Privacy
Data Privacy
本技能不收集、存储或传输任何用户数据。所有代码示例仅用于本地开发参考。
This skill does not collect, store, or transmit any user data. All code examples are provided solely as local development references.
When to use this skill
When to use this skill
- 用户说"导出 Excel"、"生成 Excel"、"下载 Excel"、"导出订单/用户/财务/库存"
- 没有预制模板,需要程序化生成结构化 Excel
- 业务上有"分 Sheet(按月份/状态/类型)"、"分 Table(多个表块)"等需求
- 需要导出图片、超链接、公式、富文本、合并单元格
- The user asks to "export Excel", "generate Excel", "download Excel", or "export orders/users/financial records/inventory"
- There is no predefined template, and structured Excel data must be generated programmatically
- The business requires multiple Sheets (such as by month, status, or type), multiple Tables (multiple table blocks), or similar layouts
- Images, hyperlinks, formulas, rich text, or merged cells need to be exported
Quick Start
Quick Start
典型调用方式(3 种写法):
java
// 写法 1:JDK8+ 用 Supplier(since 3.0.0-beta1,最简洁)
EasyExcel.write(fileName, DemoData.class)
.sheet("模板")
.doWrite(() -> data());
// 写法 2:传统 + 已有 List
List<DemoData> data = data();
EasyExcel.write(fileName, DemoData.class)
.sheet("模板")
.doWrite(data);
// 写法 3:Builder(多次写入 / 复杂配置)
try (ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build()) {
WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
excelWriter.write(data(), writeSheet);
}官方原文:"数据量建议 5000 以内"(单 sheet 一次性写入推荐上限)。
Typical invocation methods (3 approaches):
java
// 写法 1:JDK8+ 用 Supplier(since 3.0.0-beta1,最简洁)
EasyExcel.write(fileName, DemoData.class)
.sheet("模板")
.doWrite(() -> data());
// 写法 2:传统 + 已有 List
List<DemoData> data = data();
EasyExcel.write(fileName, DemoData.class)
.sheet("模板")
.doWrite(data);
// 写法 3:Builder(多次写入 / 复杂配置)
try (ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build()) {
WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
excelWriter.write(data(), writeSheet);
}Official text: "A data volume of 5,000 or fewer is recommended" (the recommended one-time write limit per Sheet).
Workflow
Workflow
Step 1. 定义数据模型 — 编写 DTO/VO,标注 等注解
Step 2. 选择写入方式 — 单次 / 多次 / 多次 循环
Step 3. 配置 Builder — 包含列选择、表头、样式、注册拦截器等
Step 4. 构造数据 — 准备 /
Step 5. 执行写入 — try-with-resources 关闭
Step 6. 验证输出 — 打开生成的 检查表头/样式/格式/合并
@ExcelPropertydoWrite()excelWriter.write()doWrite()List<DTO>List<List<String>>ExcelWriter.xlsxStep 1. Define the data model — Write a DTO/VO and apply annotations such as
Step 2. Choose a writing method — Single / multiple calls / repeated loops
Step 3. Configure the Builder — Include column selection, headers, styles, interceptor registration, and other settings
Step 4. Prepare the data — Construct /
Step 5. Perform the write — Use try-with-resources to close the
Step 6. Validate the output — Open the generated file and inspect the headers, styles, formatting, and merged cells
@ExcelPropertydoWrite()excelWriter.write()doWrite()List<DTO>List<List<String>>ExcelWriter.xlsxCritical: 注解速查
Critical: Annotation Quick Reference
| 注解 | 用途 | 关键属性 |
|---|---|---|
| 指定列名/索引/转换器 | |
| 忽略该字段 | —— |
| 类级:未标注字段不参与读写 | —— |
| 日期格式 | |
| 数字格式 | |
| 列宽 | |
| 表头行高 | |
| 内容行高 | |
| 表头样式(POI 风格) | fillPatternType, fillForegroundColor, font 等 |
| 表头字体 | fontHeightInPoints, color, bold 等 |
| 内容样式 | 同 HeadStyle |
| 内容字体 | 同 HeadFontStyle |
| 循环合并 | |
| 一次性绝对合并 | |
官方原文:默认order;优先级:Integer.MAX_VALUE>index>order。 官方原文:"不建议 index 和 name 同时用,要么只用index,要么只用name匹配"value
| Annotation | Purpose | Key attributes |
|---|---|---|
| Specifies the column name, index, or converter | |
| Ignores the field | — |
| Class-level: excludes unannotated fields from reading and writing | — |
| Date format | |
| Number format | |
| Column width | |
| Header row height | |
| Content row height | |
| Header style (POI-style) | fillPatternType, fillForegroundColor, font, etc. |
| Header font | fontHeightInPoints, color, bold, etc. |
| Content style | Same as HeadStyle |
| Content font | Same as HeadFontStyle |
| Loop merge | |
| One-time absolute merge | |
Official text:defaults toorder; priority:Integer.MAX_VALUE>index>order. Official text: "It is not recommended to use index and name together; use either index alone or name matching alone."value
Reference Library(按需加载)
Reference Library (load as needed)
详细代码与注释已拆分到 。按场景加载:
references/Detailed code and comments have been split into . Load them according to the scenario:
references/API & 基础模式
API & Basic Patterns
- references/api/3-write-modes.md — 3 种基础写入模式(最简 / 选择列 / 多次写入)
- references/api/key-classes.md — Key Classes / Methods 速查
- references/api/3-write-modes.md — 3 basic writing modes (simplest / selected columns / multiple writes)
- references/api/key-classes.md — Key Classes / Methods quick reference
模式化(表头 / 样式 / 合并 / 图片 / 富文本 / Web / 高级 / 大数据)
Patterns (headers / styles / merging / images / rich text / web / advanced / big data)
- references/patterns/header-format-style.md — 复杂表头 / 自定义样式(注解 + 拦截器)
- references/scenarios/merge-cell.md — 合并单元格
- references/scenarios/image-export.md — 图片导出(多来源 + 多图嵌入)
- references/scenarios/hyperlink-comment-formula-richtext.md — 超链接 / 批注 / 公式 / 富文本
- references/scenarios/web-download.md — Web 下载(含失败回 JSON)
- references/scenarios/advanced.md — 动态表头 / 自动列宽 / 自定义拦截器 / 03 版兼容
- references/scenarios/million-row.md — 百万级分批写入
- references/patterns/header-format-style.md — Complex headers / custom styles (annotations + interceptors)
- references/scenarios/merge-cell.md — Merged cells
- references/scenarios/image-export.md — Image export (multiple sources + multiple embedded images)
- references/scenarios/hyperlink-comment-formula-richtext.md — Hyperlinks / comments / formulas / rich text
- references/scenarios/web-download.md — Web download (including JSON fallback on failure)
- references/scenarios/advanced.md — Dynamic headers / automatic column widths / custom interceptors / 03-version compatibility
- references/scenarios/million-row.md — Batched writing for millions of rows
故障排查
Troubleshooting
- references/troubleshooting/gotchas.md — 12 条常见错误与正确做法
- references/troubleshooting/gotchas.md — 12 common errors and correct practices
References
References
- 官方文档:https://easyexcel.opensource.alibaba.com/docs/current/quickstart/write
- 官方示例:https://github.com/alibaba/easyexcel/blob/master/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java
- 项目主页:https://easyexcel.opensource.alibaba.com/
- GitHub:https://github.com/alibaba/easyexcel
- API 参考:https://easyexcel.opensource.alibaba.com/docs/current/api/
- Official documentation: https://easyexcel.opensource.alibaba.com/docs/current/quickstart/write
- Official examples: https://github.com/alibaba/easyexcel/blob/master/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java
- Project homepage: https://easyexcel.opensource.alibaba.com/
- GitHub: https://github.com/alibaba/easyexcel
- API reference: https://easyexcel.opensource.alibaba.com/docs/current/api/