XDocReport 的简单使用 操作word 替换变量,动态图片,指定操作指令(程序)扩展(转自:http://www.cnblogs.com/fish-in-sky/p/4973237.html)

简介: XDocReport 主要是操作word,如动态插入数字、汉字、图片,也可以通过指令形式去执行程序输出结果。1,模版变量定义。  1)新建word;  2)替换简单动态变量:Ctrl + F9   编辑域   选择MergeField  编辑域代码  如图:   3)替换动态图片:     4)指令扩展:2,代码/** * 根据模板导出word文件 * * @para

XDocReport 主要是操作word,如动态插入数字、汉字、图片,也可以通过指令形式去执行程序输出结果。

1,模版变量定义。

  1)新建word;

  2)替换简单动态变量:Ctrl + F9   编辑域   选择MergeField  编辑域代码

  如图:

 

  3)替换动态图片:

   

  4)指令扩展:

2,代码

/**
* 根据模板导出word文件
*
* @param reportData ReportData对象为数据对象,里面存储Map 数据
* @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);
// 文件夹不存在,创建所有文件夹
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("导出失败", e);
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
if (in != null) {
in.close();
}
} catch (IOException e) {
log.warn("文件流关闭失败", 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,测试

  @Test
public 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,结果

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>
目录
相关文章
|
2月前
|
JSON 搜索推荐 网络协议
玩转curl指令—测试简单的HTTP接口
玩转curl指令—测试简单的HTTP接口
51 0
Http 实现用户登录(mysql+html+request)
Http 实现用户登录(mysql+html+request)
|
5月前
html转word或pdf
html转word或pdf
84 0
|
5月前
|
Java
word转html
word转html
|
5月前
|
Java Apache
java word转html 报错org/apache/poi/xwpf/usermodel/IRunBody
java word转html 报错org/apache/poi/xwpf/usermodel/IRunBody
|
6月前
|
JavaScript 前端开发 网络协议
HTML基础标签 && CSS选择器 && JavaScript基础语法 && WebAPI_ && 页面设计 && HTTP协议
HTML基础标签 && CSS选择器 && JavaScript基础语法 && WebAPI_ && 页面设计 && HTTP协议
36 0
|
6月前
|
数据采集 数据挖掘 测试技术
在Objective-C中使用ASIHTTPRequest发送HTTP请求并获取HTML内容
在Objective-C中使用ASIHTTPRequest发送HTTP请求并获取HTML内容
|
7月前
|
数据可视化 网络协议
HTTP HTML 概述
HTTP HTML 概述
35 0
|
8月前
|
存储 Web App开发 网络协议
HTML&CSS Day01 功能元素与HTTP请求协议详解
HTML&CSS Day01 功能元素与HTTP请求协议详解
43 0
HTML&CSS Day01 功能元素与HTTP请求协议详解
|
8月前
|
Java
Java HTTP请求 如何获取并解析返回的HTML内容
在Java开发中,经常会遇到需要获取网页内容的情况。而HTTP请求是实现这一目标的常用方法之一。本文将介绍如何使用Java进行HTTP请求,并解析返回的HTML内容。
311 0