SpringBoot集成Poi-tl實現(xiàn)動態(tài)Word文檔生成的詳細步驟
前言
在項目開發(fā)過程中,遇到了一個需求:將用戶輸入的數(shù)據(jù)填充到給定格式的 Word 文檔中。簡單來說,就是要根據(jù)預定義的模板生成一個新的 Word 文檔,并自動填充數(shù)據(jù)。
什么是 Poi-tl?
官網(wǎng):http://deepoove.com/poi-tl/
poi-tl(Poi Template Language)是一個強大的 Word 模板引擎,能夠根據(jù) Word 模板和數(shù)據(jù)動態(tài)生成新的文檔。底層是基于 Apache POI 實現(xiàn)的。
Apache POI 提供了簡便的 API 來操作各種 Word 文檔元素(如文本、圖片、表格、頁眉、頁腳、圖表等),并允許直接操作文檔的 XML 結(jié)構(gòu)。
Poi-tl 的功能特點:
| 功能 | 描述 |
|---|---|
| 文本渲染 | 渲染模板中的文本標簽 |
| 圖片渲染 | 渲染模板中的圖片標簽 |
| 表格渲染 | 渲染模板中的表格標簽 |
| 列表渲染 | 渲染模板中的列表標簽 |
| 圖表渲染 | 渲染不同類型的圖表,包括柱狀圖、折線圖等 |
| 條件渲染 | 根據(jù)條件顯示或隱藏內(nèi)容 |
| 循環(huán)渲染 | 對文本、圖片、表格等內(nèi)容進行循環(huán)渲染 |
| 表格行/列循環(huán) | 對表格的行或列進行循環(huán)渲染 |
| 圖片替換 | 替換文檔中的指定圖片 |
| 超鏈接、書簽、錨點 | 支持文檔內(nèi)的超鏈接、書簽、錨點等功能 |
| 強大的表達式支持 | 完全支持 SpringEL 表達式,并可擴展至 OGNL、MVEL 等表達式 |
| 自定義標簽支持 | 支持自定義標簽前后綴 |
| 文本框支持 | 支持文本框中的標簽 |
| 樣式自定義 | 自定義模板樣式,同時代碼中也可動態(tài)設置樣式 |
| 模板嵌套 | 支持嵌套模板功能 |
| Word 合并 | 支持 Word 中的合并操作 |
| 自定義函數(shù)(插件) | 允許在模板中使用自定義函數(shù) |
通過這些特性,我們可以輕松地實現(xiàn)模板渲染,生成符合特定格式要求的文檔。
如何使用 Poi-tl?
本文以 Spring Boot 項目為例,展示如何集成 Poi-tl 進行模板渲染。小伙伴們可以按照步驟一起實踐!
1. 創(chuàng)建 Spring Boot 項目
首先,創(chuàng)建一個 Spring Boot 項目,版本為 2.2.1(可以根據(jù)需要調(diào)整版本)。在 pom.xml 文件中引入以下依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2. 添加 Poi-tl 依賴
接下來,向 pom.xml 中添加 Poi-tl 的依賴:
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.9.1</version>
</dependency>
3. 準備 Word 模板
我們需要準備一個 Word 模板??梢允謩觿?chuàng)建一個 Word 文件,這里我們以 Hello World.docx 為例,模板內(nèi)容如下:

我們將模板存放在 resources 目錄下,并定義幾個占位符,例如:{{title}}、{{text}}、{{author}} 等。
4. 渲染模板并生成新文檔
我們可以通過 XWPFTemplate 來渲染模板,將數(shù)據(jù)填充到模板中并生成新的文檔。示例代碼如下:
@SpringBootTest
public class PoiTlApplicationTest {
@Test
public void test() {
// 獲取 Word 模板路徑
String filepath = this.getClass().getClassLoader().getResource("hello-world.docx").getPath();
// 渲染數(shù)據(jù)
XWPFTemplate template = XWPFTemplate.compile(filepath).render(
new HashMap<String, Object>() {{
put("title", "Hello, poi-tl Word模板引擎");
put("text", "Hello World");
}}
);
// 輸出新文檔
try {
template.writeAndClose(new FileOutputStream("output.docx"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
執(zhí)行上述單元測試后,新的文檔 output.docx 將會生成,并且內(nèi)容將如預期渲染。

相關(guān)概念
模板
模板是一個 .docx 格式的文件,通常通過 Microsoft Word 或 WPS Office 創(chuàng)建。在模板中,我們使用占位符來標識需要動態(tài)渲染的內(nèi)容。
標簽
Poi-tl 使用標簽來標識占位符,所有標簽都以 {{ 開頭,}} 結(jié)尾。標簽可以出現(xiàn)在 Word 文檔的任何位置,包括頁眉、頁腳、表格內(nèi)部、文本框等。
數(shù)據(jù)模型
數(shù)據(jù)模型是將渲染數(shù)據(jù)傳遞給模板的方式,通常使用 Map 或 Java 對象。我們可以選擇通過哈希表或?qū)ο髞韨鬟f數(shù)據(jù)。
- 哈希表(key 為標簽名):
Map<String, Object> data = new HashMap<>();
data.put("title", "Hello, poi-tl Word模板引擎");
data.put("text", "Hello World");
data.put("author", "god23bin");
data.put("description", "這還不關(guān)注 god23bin?再不關(guān)注我可要求你關(guān)注了!");
- Java 對象(屬性為標簽名):
public class DataModel {
private String title;
private String text;
private String author;
private String description;
// 省略 getter 和 setter
}
DataModel data = new DataModel();
data.setTitle("Hello, poi-tl Word模板引擎");
data.setText("Hello World");
data.setAuthor("god23bin");
data.setDescription("這還不關(guān)注 god23bin?再不關(guān)注我可要求你關(guān)注了!");
標簽寫法
Poi-tl 支持多種標簽類型,常見的標簽包括文本、圖片、表格、列表等:
- 文本標簽:
{{var}} - 圖片標簽:
{{@var}} - 表格標簽:
{{#var}} - 列表標簽:
{{*var}}
插件
Poi-tl 支持插件機制,插件允許我們在模板中執(zhí)行自定義的邏輯。例如,我們可以將 HTML 渲染插件綁定到某個標簽上,使其支持 HTML 內(nèi)容渲染。
Configure configure = Configure.builder().bind("description", new HtmlRenderPolicy()).build();
表格行循環(huán)
如果需要在 Word 文檔中渲染多行表格數(shù)據(jù),可以使用 HackLoopTableRenderPolicy 插件來循環(huán)渲染表格的行。
@Test
public void rowLoopTest() {
AcWordModel data = getFromDB(); // 假設從數(shù)據(jù)庫獲取數(shù)據(jù)
String filepath = this.getClass().getClassLoader().getResource("table-row-loop.docx").getPath();
Configure configure = Configure.builder()
.bind("articles", new HackLoopTableRenderPolicy())
.bind("columns", new HackLoopTableRenderPolicy())
.build();
XWPFTemplate template = XWPFTemplate.compile(filepath, configure).render(data);
try {
template.writeAndClose(new FileOutputStream("ac-word.docx"));
} catch (IOException e) {
e.printStackTrace();
}
}
封裝工具類
為了方便調(diào)用,我們可以封裝一個工具類來處理 Word 渲染:
public class WordUtil {
public static void generateWordFile(String templatePath, String targetPath, Map<String, Object> data) {
XWPFTemplate template = XWPFTemplate.compile(templatePath).render(data);
try {
template.writeAndClose(new FileOutputStream(targetPath));
} catch (IOException e) {
e.printStackTrace();
}
}
}
總結(jié)
通過 Poi-tl,我們可以輕松實現(xiàn)基于模板的文檔渲染。無論是簡單的文本填充,還是復雜的表格、圖表渲染,都能輕松應對。希望這篇教程能幫助大家更好地使用 Poi-tl 進行文檔自動化生成!
以上就是SpringBoot集成Poi-tl實現(xiàn)動態(tài)Word文檔生成的詳細步驟的詳細內(nèi)容,更多關(guān)于SpringBoot Poi-tl動態(tài)Word文檔生成的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
解決spring-boot-maven-plugin報紅的問題
這篇文章主要給大家介紹一下如何解決spring-boot-maven-plugin報紅的問題,文中通過圖文講解的非常詳細,具有一定的參考價值,需要的朋友可以參考下2023-08-08
關(guān)于Spring Boot動態(tài)權(quán)限變更問題的實現(xiàn)方案
這篇文章主要介紹了Spring Boot動態(tài)權(quán)限變更實現(xiàn)的整體方案使用session作為緩存,結(jié)合AOP技術(shù)進行token認證和權(quán)限控制,本文給大家介紹的非常詳細,需要的朋友參考下吧2021-06-06
java在linux本地執(zhí)行shell命令的實現(xiàn)方法
本文主要介紹了java在linux本地執(zhí)行shell命令的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02

