XDocReport 的简单使用 操作word 替换变量,动态图片,指定操作指令(程序)扩展(转自:http://www.cnblogs.com/fish-in-sky/p/4973237.html)
生活随笔
收集整理的這篇文章主要介紹了
XDocReport 的简单使用 操作word 替换变量,动态图片,指定操作指令(程序)扩展(转自:http://www.cnblogs.com/fish-in-sky/p/4973237.html)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
XDocReport 主要是操作word,如動態(tài)插入數(shù)字、漢字、圖片,也可以通過指令形式去執(zhí)行程序輸出結(jié)果。
1,模版變量定義。
? 1)新建word;
? 2)替換簡單動態(tài)變量:Ctrl + F9 ??編輯域 ? 選擇MergeField ?編輯域代碼
? 如圖:
?
? 3)替換動態(tài)圖片:
? ?
? 4)指令擴(kuò)展:
2,代碼
/*** 根據(jù)模板導(dǎo)出word文件
*
* @param reportData ReportData對象為數(shù)據(jù)對象,里面存儲Map 數(shù)據(jù)
* @param templateName 模板文件路徑
* @param outputFileName 輸出文件路徑
*/
public static void reportDoc(ReportData reportData, String templateName, String outputFileName) {
Map<String, Object> params = reportData.getParameters();
InputStream in = null;
OutputStream outputStream = null;
IXDocReport report = null;
try {
// 1) Load ODT file and set Velocity template engine and cache it to the registry
in = new FileInputStream(new File(StringUtil.formatFilePath(templateName)));
// 2) Create Java model context
IContext context = getReportContext(report, params);
// 輸出文件,文件存在則刪除
File outputFile = new File(outputFileName);
// 文件夾不存在,創(chuàng)建所有文件夾
File parentFile = outputFile.getParentFile();
if (!parentFile.exists()) {
parentFile.mkdirs();
}
if (outputFile.exists()) {
outputFile.renameTo(new File(outputFileName + "." + new Date().getTime()));
}
// 生成新的文件
outputStream = new FileOutputStream(outputFileName);
report.process(context, outputStream);
} catch (IOException e) {
log.warn("文件流獲取失敗", e);
} catch (XDocReportException e) {
log.warn("導(dǎo)出失敗", e);
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
if (in != null) {
in.close();
}
} catch (IOException e) {
log.warn("文件流關(guān)閉失敗", e);
}
}
} private static IContext getReportContext(IXDocReport report, Map<String, Object> params) throws XDocReportException {
IContext context = null;
if (report != null) {
context = report.createContext();
FieldsMetadata metadata = new FieldsMetadata();
for (Iterator iterator = params.entrySet().iterator(); iterator.hasNext(); ) {
Map.Entry entry = (Map.Entry) iterator.next();
String name = StringUtil.obj2Str(entry.getKey());
Object value = entry.getValue();
context.put(name, value);
}
report.setFieldsMetadata(metadata);
}
return context;
}
3,測試
@Testpublic void testXDocWord() throws Exception {
ReportData reportData = new ReportData();
reportData.addParameters("name", "張三");
reportData.addParameters("age", "2016-6-6");
XDocReport.reportDoc(reportData, "D:\\tempword\\template.docx", "D:\\tempword\\t.docx");
}
4,結(jié)果
5,pom
<dependency><groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.converter.docx.xwpf</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.itext.extension</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.11</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.11</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.1</version>
</dependency>
總結(jié)
以上是生活随笔為你收集整理的XDocReport 的简单使用 操作word 替换变量,动态图片,指定操作指令(程序)扩展(转自:http://www.cnblogs.com/fish-in-sky/p/4973237.html)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 联想笔记本怎么装win10系统 联想笔记
- 下一篇: 怎么在pe修复磁盘驱动器 PE系统磁盘驱