1、Maven依賴
<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>
2、.docx或.doc格式的word模板準(zhǔn)備
- 創(chuàng)建.docx文件,編寫內(nèi)容
- 添加編輯域
按Ctrl+F9,創(chuàng)建編輯域
右擊,選擇編輯域
選擇郵件合并,修改域代碼
要注意域代碼的格式為:MERGEFIELD ${name}
name為需要填充的內(nèi)容。
????????依次類推,填上所有的編輯域,調(diào)整文檔格式等,就完成了word文檔準(zhǔn)備。
????????之后需要代碼讀word模板轉(zhuǎn)數(shù)據(jù)流,進(jìn)行代碼域
的變量批量替換,重新寫文件就完成的word文件生成工作。文章來源:http://www.zghlxwxcb.cn/news/detail-683889.html
3、讀word模板,批量替換代碼域,生成文件,demo
package com.dongzi.utils.word;
import fr.opensagres.xdocreport.core.XDocReportException;
import fr.opensagres.xdocreport.document.IXDocReport;
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
import fr.opensagres.xdocreport.template.IContext;
import fr.opensagres.xdocreport.template.TemplateEngineKind;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
/**
* 讀word模板,生成數(shù)據(jù)
*/
public class ReadWordTemplate {
public static void main(String[] args) throws IOException, XDocReportException {
// 從resources/template中獲取word模板數(shù)據(jù)
IXDocReport ixDocReport = readWord("info.docx");
IContext context = ixDocReport.createContext();
// set value
// putTemplateValue_1(context);
// putTemplateValue_2(context);
putTemplateValue_3(context);
// =============
FileOutputStream out = new FileOutputStream("D:/temp/docx模板輸出.docx");
ixDocReport.process(context, out);
out.flush();
out.close();
}
public static IXDocReport readWord(String fileName) throws IOException, XDocReportException {
// 讀模板的方式
// 方式1:通過URL加載
// URL url = ClassLoader.getSystemClassLoader().getResource("template/" + fileName);
// assert url != null;
// InputStream in_1 = url.openStream();
// 方式2:系統(tǒng)資源轉(zhuǎn)數(shù)據(jù)流
InputStream in_2 = ClassLoader.getSystemResourceAsStream("template/" + fileName);
// 方式3:spring的類加載器,獲取資源
// ClassPathResource pathResource = new ClassPathResource("template/" + fileName);
// InputStream in_3 = pathResource.getInputStream();
return XDocReportRegistry.getRegistry().loadReport(in_2, TemplateEngineKind.Freemarker);
}
// 設(shè)置docx模板值
public static void putTemplateValue_1(IContext context) {
// docx文檔模板讀取,必須要預(yù)先再模板里面設(shè)置文本域,Ctrl+F9
/*
1. Ctrl+F9,打開編輯域
2. 域選擇“郵件合并”,域代碼為:MERGEFIELD ${yourAddress}
3. 頁面展示的格式為:?${yourAddress}?,模板可替換
*/
context.put("yourName", "孫悟空");
context.put("yourAge", "500");
context.put("yourAddress", "花果山水簾洞");
}
public static void putTemplateValue_2(IContext context) {
// 生成數(shù)據(jù)
Map<String, Object> mapValues = new HashMap<>();
mapValues.put("yourName", "齊天大圣");
mapValues.put("yourAge", 36000);
mapValues.put("yourAddress", "花果山水簾洞");
//
context.putMap(mapValues);
}
public static void putTemplateValue_3(IContext context) {
// 生成數(shù)據(jù)
Map<String, Object> mapValues = new HashMap<>();
mapValues.put("yourName", "派大星");
mapValues.put("yourAge", 36000);
mapValues.put("yourAddress", "黃土高坡");
for (int i = 1; i <= 10; i++) {
mapValues.put("index_" + i, i);
mapValues.put("value_" + i, i + 2);
}
//
context.putMap(mapValues);
}
}
4、結(jié)果展示
至此,整個(gè)模板讀取生成過程全部結(jié)束。文章來源地址http://www.zghlxwxcb.cn/news/detail-683889.html
到了這里,關(guān)于讀word模板批量生成制式文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!