-
Notifications
You must be signed in to change notification settings - Fork 324
Excel template build
清沐 edited this page Apr 2, 2022
·
4 revisions
All excel builders cannot be singleton, please pay attention!!!
The core principle of template build is to render HTML table elements that meet the layout requirements according to the template engine, and the builder iterates the cells in the table elements to render excel cells.
-
The following template engines are not imported by default. Users can choose to declare the import in pom.xml according to their own needs.
-
The following template engine version is the minimum version number.
The following template engine version is the lowest version number.
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl</artifactId>
<version>2.7.23</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-templates</artifactId>
<version>2.4.13</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>com.jfinal</groupId>
<artifactId>enjoy</artifactId>
<version>4.8</version>
</dependency>
- When an HTML file already exists, in this way, the HTML file is not limited to the classpath (such as resources) of the project, nor does it need a template engine.
// get html file
File htmlFile = new File("/Users/liaochong/Downloads/example.html");
// read the html file and use default excel style to create excel
Workbook workbook = HtmlToExcelFactory.readHtml(htmlFile).useDefaultStyle().build();
// this is a example,you can write the workbook to any valid outputstream
FileExportUtil.export(workbook, new File("/Users/liaochong/Downloads/excel.xlsx"));
- Use the built-in template engine excel builder such as FreeMarker. The template file should be stored in classpath. Please refer to the example in the project for details.
/**
* use non-default-style excel builder
* Template files are placed under Resources
*
* @param response response
*/
@GetMapping("/freemarker/example")
public void build(HttpServletResponse response) {
Map<String, Object> dataMap = this.getDataMap();
try (ExcelBuilder excelBuilder = new FreemarkerExcelBuilder()) {
Workbook workbook = excelBuilder
// fileTemplate(dirPath,fileName)
.classpathTemplate("/templates/freemarkerToExcelExample.ftl")
.build(dataMap);
AttachmentExportUtil.export(workbook, "freemarker_excel", response);
}
}
/**
* use default-style excel builder
* Template files are placed under Resources
*
* @param response response
*/
@GetMapping("/freemarker/defaultStyle/example")
public void buildWithDefaultStyle(HttpServletResponse response) {
Map<String, Object> dataMap = this.getDataMap();
try (ExcelBuilder excelBuilder = new FreemarkerExcelBuilder()){
Workbook workbook = excelBuilder
// fileTemplate(dirPath,fileName)
.classpathTemplate("/templates/freemarkerToExcelExample.ftl")
.useDefaultStyle()
.build(dataMap);
AttachmentExportUtil.export(workbook, "freemarker_excel", response);
}
}
private Map<String, Object> getDataMap() {
Map<String, Object> dataMap = new HashMap<>();
dataMap.put("sheetName", "freemarker_excel_example");
List<String> titles = new ArrayList<>();
titles.add("Category");
titles.add("Product Name");
titles.add("Count");
dataMap.put("titles", titles);
List<Product> data = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Product product = new Product();
if (i % 2 == 0) {
product.setCategory("蔬菜");
product.setName("小白菜");
product.setCount(100);
} else {
product.setCategory("电子产品");
product.setName("ipad");
product.setCount(999);
}
data.add(product);
}
dataMap.put("data", data);
return dataMap;
}
<table>
<caption>${sheetName}</caption>
<thead>
<tr style="background-color: #6495ED">
<th colspan="3" style="text-align: center;vertical-align: middle;font-weight: bold;font-size: 14px;">产品介绍</th>
</tr>
<tr>
<#list titles as title>
<th>${title}</th>
</#list>
</tr>
</thead>
<tbody>
<#list data as item>
<tr>
<td>${item.category}</td>
<td>${item.name}</td>
<td>${item.count}</td>
<td url>百度地址</td>
</tr>
</#list>
</tbody>
</table>
-
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
样式支持 - 添加水印
- 按列读取
- 单元格斜线绘制
- 设置批注
- 版本日志