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>
目录
相关文章
|
JSON 搜索推荐 网络协议
玩转curl指令—测试简单的HTTP接口
玩转curl指令—测试简单的HTTP接口
618 0
|
安全 JavaScript Go
Vue中的v-html指令有什么潜在的安全风险?如何防范?
Vue中的v-html指令有什么潜在的安全风险?如何防范?
1181 1
|
6月前
|
前端开发
“弘五四,耀青春”程序创意获奖作品【html+css】
本作品以“青春筑梦,共创未来”为主题,采用动态龙元素展现青春活力与创新精神。页面设计简洁明快,色彩协调,突显年轻人积极向上的风貌。作品内容包括获奖截图、名字《时代扬新帆》及源代码分享。特别说明:禁止用于商业活动,可用于比赛和作业等开源场景。最后,作者表达了对五四精神的致敬与传承,强调了青春活力和创造力的重要性,并感谢评委和支持者。 **获奖感言摘录:** “获得这个奖项,对我们团队来说,既是认可也是激励。我们将继续努力,不断优化产品,为用户带来更好的体验,为社会贡献更多价值。”
118 2
|
9月前
|
JavaScript 前端开发 安全
vue -- 指令 -- v-text/html/on/show/if/bind/for/model
【10月更文挑战第17天】Vue 指令是构建 Vue 应用的基础工具,掌握它们的特性和用法是成为一名优秀 Vue 开发者的重要一步。通过深入理解和熟练运用这些指令,可以打造出更加出色的前端应用。
129 50
|
Ubuntu C++ Docker
Docker的基本指令和HTML/PYTHON/C++的简单创建示例
Docker的基本指令和HTML/PYTHON/C++的简单创建示例
|
8月前
|
存储 移动开发 前端开发
高效的 HTML 与 CSS 编写技巧,涵盖语义化标签、文档结构优化、CSS 预处理、模块化设计、选择器优化、CSS 变量、媒体查询等内容
本文深入探讨了高效的 HTML 与 CSS 编写技巧,涵盖语义化标签、文档结构优化、CSS 预处理、模块化设计、选择器优化、CSS 变量、媒体查询等内容,旨在提升开发效率、网站性能和用户体验。
226 5
|
消息中间件 API 数据库
在微服务架构中,每个服务通常都是一个独立运行、独立部署、独立扩展的组件,它们之间通过轻量级的通信机制(如HTTP/RESTful API、gRPC等)进行通信。
在微服务架构中,每个服务通常都是一个独立运行、独立部署、独立扩展的组件,它们之间通过轻量级的通信机制(如HTTP/RESTful API、gRPC等)进行通信。
|
网络协议 应用服务中间件 nginx
Nginx的http块sendfile,keepalive_timeout的配置指令说明
Nginx的http块sendfile,keepalive_timeout的配置指令说明
|
JavaScript 前端开发
基于 Node.js 环境,使用内置 http 模块,创建 Web 服务程序
基于 Node.js 环境,使用内置 http 模块,创建 Web 服务程序
|
JavaScript 前端开发 物联网
文本,Vue实现打印的方式,打印机的种类有多少,浏览器打印html,右键,2打印插件,3指令打印,vue-print-nb
文本,Vue实现打印的方式,打印机的种类有多少,浏览器打印html,右键,2打印插件,3指令打印,vue-print-nb

热门文章

最新文章