java如何根據(jù)模板導(dǎo)出數(shù)據(jù)到word文檔中(表格、自定義標(biāo)簽等)
1、制作模板
1.1 創(chuàng)建docx文檔,并且創(chuàng)建表格

1.2 配置模板信息
將鼠標(biāo)的光標(biāo)移到要輸入數(shù)據(jù)的地方,點(diǎn)擊“插入”,點(diǎn)擊“文檔部件”,點(diǎn)擊“域”;選擇“郵件合并”,在域代碼的函數(shù)后加入自定義的字段名“${字段名}”。如果不用循環(huán)的話,直接字段名字節(jié)定義名稱就可以了,如果需要循環(huán),字段名設(shè)置為【${對象.屬性}】



若需要循環(huán)表格內(nèi)的內(nèi)容時,處理如下,“域”中“郵件合并”的域代碼使用“${對象.屬性}”格式,自此,一個簡單的模板制作完成



2、代碼實現(xiàn)
2.1 pom.xml引入依賴
XDocReport +FreeMarker,該技術(shù)組合既簡單又高效可實現(xiàn)word模板的編輯,docx和doc均可處理
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.core</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.document</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.template</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.document.docx</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.template.freemarker</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
2.2 創(chuàng)建接收表格內(nèi)容的對象(也可以沒必要,在下面封裝中,也可以用map封裝)
package com.yhgc.domain;
import lombok.Data;
@Data
public class UserInfo {
/** 姓名 */
public String name;
/** 性別 */
public String sex;
/** 年齡*/
public String age;
}
2.3 調(diào)用的實現(xiàn)
@Test
public void testWord(){
InputStream ins = null;
OutputStream out = null;
try {
//獲取Word模板,模板存放路徑
ins = new FileInputStream("E:\\export\\template\\模板1.docx");
//注冊xdocreport實例并加載FreeMarker模板引擎
IXDocReport report = XDocReportRegistry.getRegistry().loadReport(ins, TemplateEngineKind.Freemarker);
//創(chuàng)建xdocreport上下文對象,用于存放具體數(shù)據(jù)
IContext context = report.createContext();
//創(chuàng)建要替換的文本變量
List<UserInfo> userInfos = new ArrayList<>(); // 這里也可以使用Map集合
UserInfo userInfo = new UserInfo();
userInfo.setName("張三");
userInfo.setAge("15");
userInfo.setSex("男");
userInfos.add(userInfo);
UserInfo userInfo1 = new UserInfo();
userInfo1.setName("李四");
userInfo1.setAge("14");
userInfo1.setSex("男");
userInfos.add(userInfo1);
UserInfo userInfo2 = new UserInfo();
userInfo2.setName("李梅");
userInfo2.setAge("16");
userInfo2.setSex("女");
userInfos.add(userInfo2);
//此處的userInfo是word中命名的列表名
context.put("userInfo", userInfos);
context.put("title", "測試的標(biāo)題");
//創(chuàng)建字段元數(shù)據(jù)
FieldsMetadata fm = report.createFieldsMetadata();
//Word模板中的表格數(shù)據(jù)對應(yīng)的集合類型
fm.load("userInfo", UserInfo.class, true);
//輸出到本地目錄
String filePath = "e:\\export\\結(jié)果.docx";
out = new FileOutputStream(new File(filePath));
report.process(context, out);
} catch (Exception e) {
System.out.println("生成word發(fā)生異常"+e);
}finally {
try {
if (ins != null){
ins.close();
}
if (out != null){
out.close();
}
} catch (IOException e) {
System.out.println("文件流關(guān)閉失敗"+e);
}
}
}
2.4 實現(xiàn)效果

總結(jié)
到此這篇關(guān)于java如何根據(jù)模板導(dǎo)出數(shù)據(jù)到word文檔中的文章就介紹到這了,更多相關(guān)java根據(jù)模板導(dǎo)出數(shù)據(jù)到word內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java報錯org.hibernate.TypeMismatchException的解決方法
在Java開發(fā)領(lǐng)域,尤其是涉及到數(shù)據(jù)持久化的項目中,Hibernate是一款廣泛使用的強(qiáng)大工具,然而,可能會在使用過程中遭遇各種報錯,其中org.hibernate.TypeMismatchException就是一個讓人頭疼的問題,下面我們一起深入剖析這個報錯信息2024-11-11
梳理總結(jié)Java?static關(guān)鍵字的方法作用
這篇文章主要介紹了梳理總結(jié)Java?static關(guān)鍵字的方法作用,?static?關(guān)鍵字可以用來修飾的成員變量和成員方法,被修飾的成員是屬于類的,而不是單單是屬于某個對象的2022-06-06
詳解Java synchronized關(guān)鍵字的用法
在多線程編程中常常使用鎖機(jī)制來確保同一時刻只有一個線程能夠修改共享內(nèi)存,在Java中一般是使用synchronized作為鎖機(jī)制,下面就讓我們來學(xué)習(xí)一下如何使用synchronized實現(xiàn)線程安全吧2023-08-08
SpringBoot 整合Tess4J庫實現(xiàn)圖片文字識別案例詳解
Tess4J是一個基于Tesseract OCR引擎的Java接口,可以用來識別圖像中的文本,說白了,就是封裝了它的API,讓Java可以直接調(diào)用,今天給大家分享一個SpringBoot整合Tess4j庫實現(xiàn)圖片文字識別的小案例2023-10-10

