当前位置: 首页 > backend >正文

使用xdocreport导出word

之前java总用freemaker进行导出,但是改xml实在是太繁琐了,这次找了另一个工具进行体验.

一、简单导出

pom引入

 <dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>fr.opensagres.xdocreport.core</artifactId><version>2.0.6</version></dependency><dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>fr.opensagres.xdocreport.document</artifactId><version>2.0.6</version></dependency><dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>fr.opensagres.xdocreport.template</artifactId><version>2.0.6</version></dependency><dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>fr.opensagres.xdocreport.document.docx</artifactId><version>2.0.6</version></dependency><dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>fr.opensagres.xdocreport.template.freemarker</artifactId><version>2.0.6</version></dependency>

导出代码

public void genDoc() {RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();ServletRequestAttributes attributes = (ServletRequestAttributes) requestAttributes;HttpServletResponse response = attributes.getResponse();try (InputStream inputStream = POICacheManager.getFile("template.docx")) {// 3. 读取模板,创建 IXDocReport 对象IXDocReport report = XDocReportRegistry.getRegistry().loadReport(inputStream, TemplateEngineKind.Freemarker);// 4. 创建上下文,设置变量(你可以按需扩展)IContext context = report.createContext();context.put("name", "张三");//编辑域代码 ctrl F9 类别选 邮件合并 域名mergefield 域代码后面填 ${name}// 设置响应头,准备输出 Word 文件response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");response.setHeader("Content-Disposition", "attachment; filename=generated-document.docx");// 5. 生成 Word 并写入响应输出流try (OutputStream out = response.getOutputStream()) {report.process(context, out);}} catch (Exception e) {log.error(">>> 生成 Word 失败:", e);// 手动返回错误信息try {response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);response.getWriter().write("生成文档失败:" + e.getMessage());} catch (Exception ex) {log.error(">>> 写回错误信息失败", ex);}}}

在resource目录新建word文件名为template.docx

打开template.docx

输入任意字符,然后 Ctrl+9插入域,然后右键编辑域,类别选 邮件合并 域名选mergefield 域代码后面填 ${name} 保存后用上面的代码导出

导出后可以看到域代码位置被填充为张三了。

二、进阶用法

1.动态表格行

分两种情况处理

(1)第一列为序号列

将第一列填充3个域  

"@before-row[#list productInfoList as item]" 
${item?index+1} 
@after-row[/#list] 

第二列 填充实际的字段即可

${item.name}

(2)第一列不序号列

将上文中的序号表达式${item?index+1} 换成你想填充的表达式,如

${item.code} 

其他处理跟上卖弄一样

在java中的定义,先定义实体,然后context.put即可

//假设之前定义了productInfo实体,实体有code name等字段
List<ProductInfo> productInfoList = new ArrayList();
...context.put("productInfoList", productInfoList );

2.图片

图片不用域,在要导入的地方插入一张图片,然后建立书签,建立书签的方法如下

选中文档中的某张图片,单击【插入】菜单下【链接】子菜单中的【书签】。

这里的书签名跟上面的域变量名类似

java代码

//根据文件读取
IImageProvider iImageProvider = new FileImageProvider(file, false);
context.put("pic", iImageProvider);//dataurlbase64的字符串,不含前缀
byte[] imageBytes = Base64.getDecoder().decode(base64Image);
//创建 ByteArrayImageProvider 实例
ByteArrayImageProvider imageProvider = new ByteArrayImageProvider(imageBytes, "png");
context.put("pic", imageProvider );

3.条件显示隐藏

使用if标签,下面是3个域

[#if data.ccUser??] 申请人:${data.ccUser } [/#if]

可以看到两个if标签域包裹了文字和变量

4.空值处理

空值直接使用会报错,可使用表达式

${tx.amount!}

或者

${tx.amount?if_exists}

上面表达式表示有值填充,无值不填充

5.单元格合并

实现起来比较复杂,在模板中先合并,然后对多列使用第一节的动态表格行基本上可以实现按层级合并,如果是跨列合并,比较复杂的合并目前还没找到好的解决方案?或许需要考虑原生freemaker或者poi-tl?

6.关键字

在实体中应尽量避免使用以下关键字

length 

http://www.xdnf.cn/news/12418.html

相关文章:

  • c++ map与multiset的介绍
  • JUnit​​ 和 ​​Mockito​​ 的详细说明及示例,涵盖核心概念、常用注解、测试场景和实战案例。
  • 集群与分布式与微服务
  • 软件测试:质量保障的基石与未来趋势
  • 计算机网络(6)——局域网
  • leetcode1971. 寻找图中是否存在路径-easy
  • 自托管图书搜索引擎Bookologia
  • EasyRTC嵌入式音视频通信SDK助力物联网/视频物联网音视频打造全场景应用
  • 6.6 day38
  • 现实生活例子[特殊字符] 通俗易懂的解释[特殊字符] JS中的原型和原型链[特殊字符]
  • AC68U刷梅林384/386版本后不能 降级回380,升降级解决办法
  • 一个WebRTC 分辨率动态爬升问题记录与解决过程
  • SQLServer中的存储过程与事务
  • Kafka 快速上手:安装部署与 HelloWorld 实践(二)
  • Kafka 快速上手:安装部署与 HelloWorld 实践(一)
  • uniapp 设置手机不息屏
  • Go 中 map 的双值检测写法详解
  • 从零实现STL哈希容器:unordered_map/unordered_set封装详解
  • Transformer-BiGRU多变量时序预测(Matlab完整源码和数据)
  • Python概率统计可视化——概率分布、假设检验与分子运动模型
  • GNSS终端授时方式-合集:PPS、B码、NTP、PTP、单站授时,共视授时
  • Go 中的 Map 与字符处理指南
  • Transformer架构解析:Encoder与Decoder核心差异、生成式解码技术详解
  • Python读取PDF:文本、图片与文档属性
  • Linux文件系统详解:从入门到精通
  • Chrome书签的导出与导入:步骤图
  • 高温IC设计带来的挑战和问题
  • Java + Spring Boot + Mybatis 实现批量插入
  • 96. 2017年蓝桥杯省赛 - Excel地址(困难)- 进制转换
  • 大数据学习(131)-Hive数据分析函数总结