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ù)的地方,點擊“插入”,點擊“文檔部件”,點擊“域”;選擇“郵件合并”,在域代碼的函數(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連接mysql數(shù)據(jù)庫的五種方式
這篇文章主要介紹了詳解java連接mysql數(shù)據(jù)庫的五種方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Java?深入理解創(chuàng)建型設(shè)計模式之原型模式
原型(Prototype)模式的定義如下:用一個已經(jīng)創(chuàng)建的實例作為原型,通過復(fù)制該原型對象來創(chuàng)建一個和原型相同或相似的新對象。在這里,原型實例指定了要創(chuàng)建的對象的種類。用這種方式創(chuàng)建對象非常高效,根本無須知道對象創(chuàng)建的細(xì)節(jié)2022-02-02
Java實現(xiàn)的模糊匹配某文件夾下的文件并刪除功能示例
這篇文章主要介紹了Java實現(xiàn)的模糊匹配某文件夾下的文件并刪除功能,涉及java針對目錄與文件的遍歷、匹配、判斷、刪除等相關(guān)操作技巧,需要的朋友可以參考下2018-02-02
Java日常練習(xí)題,每天進(jìn)步一點點(23)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望可以幫到你2021-07-07
SpringBoot對接第三方系統(tǒng)的實現(xiàn)
本文主要介紹了在Spring Boot中選擇合適的HTTP客戶端方案以及數(shù)據(jù)同步方案,HTTP客戶端方案包括RestTemplate、Feign和WebClient,適用于不同場景,數(shù)據(jù)同步方案分為定時全量同步、定時增量同步和實時同步,感興趣的可以了解一下2026-02-02
java 二進(jìn)制數(shù)據(jù)與16進(jìn)制字符串相互轉(zhuǎn)化方法
今天小編就為大家分享一篇java 二進(jìn)制數(shù)據(jù)與16進(jìn)制字符串相互轉(zhuǎn)化方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07

