XDocReport 的简单使用 操作word 替换变量,动态图片,指定操作指令(程序)扩展(转自:http://www.cnblogs.com/fish-in-sky/p/4973237.html)
来源:互联网 发布:洛丽塔电影知乎 编辑:程序博客网 时间:2024/06/07 20:32
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>
0 0
- XDocReport 的简单使用 操作word 替换变量,动态图片,指定操作指令(程序)扩展(转自:http://www.cnblogs.com/fish-in-sky/p/4973237.html)
- redis的使用(转载自:http://www.cnblogs.com/edisonfeng/p/3571870.html)
- 有关uploadifive的使用经验(转:http://www.cnblogs.com/itBscs/p/4781786.html )
- POI操作 http://www.cnblogs.com/dayuruozhi/p/6490793.html
- Linux常用命令(转自:http://www.cnblogs.com/laov/p/3541414.html)
- python数据类型详解(转自:http://www.cnblogs.com/linjiqin/p/3608541.html)
- (转自:http://www.cnblogs.com/findumars/p/5180490.html)内存管理
- 详解Inten t转自(http://www.cnblogs.com/engine1984/p/4146621.html)
- IntelliJ IDEA使用(二):tomcat和jetty配置(转自:http://www.cnblogs.com/jenkinschan/p/6052948.html)
- 如何使用Fiddler调试线上JS代码(转自:http://www.cnblogs.com/RockLi/p/3511132.html)
- fiddler几种功能强大的用法(一)转自:http://www.cnblogs.com/chenshaoping/p/5785010.html
- linux找不到动态链接库 .so文件的解决方法(转自:http://www.cnblogs.com/xudong-bupt/p/3698294.html)
- 图像的矩特征(转自http://www.cnblogs.com/pengkunfan/p/3998921.html)
- Zookeeper的功能以及工作原理 (转自:http://www.cnblogs.com/felixzh/p/5869212.html)
- FCN网络的训练 -- 转自http://www.cnblogs.com/xuanxufeng/p/6243342.html
- PHP程序员要看的书单 转自http://www.cnblogs.com/piwefei/p/6900491.html
- [Python] heapq简介(转载自:http://www.cnblogs.com/lexus/p/3324991.html)
- Verilog中wire与reg类型的区别(转载自http://www.cnblogs.com/farbeyond/p/5204586.html)
- 2017趣加全栈工程师暑期实习招聘笔试题
- SAP CRM 在Web UI中创建搜索帮助
- Ruby on Rails学习笔记(四 用scaffold实现持久化类)
- Linux用户和组管理命令
- 人脸检测(一)
- XDocReport 的简单使用 操作word 替换变量,动态图片,指定操作指令(程序)扩展(转自:http://www.cnblogs.com/fish-in-sky/p/4973237.html)
- python循环
- TCP粘包处理
- 数论总结笔记
- Linux网络管理命令
- Handsontable 鼠标移上去改变行颜色
- 关于Attempt to invoke virtual method 'HSSFCell.getCellType(int)'' on a null object reference
- <%@include%>和<jsp:include>的区别
- 【编译原理】编译原理简单介绍