Java使用FreeMarker模版技术动态生成word实践

Java使用FreeMarker模版技术动态生成word实践
摘要由CSDN通过智能技术生成

一、序言

在日常开发中,常常有动态word文件生成的需求,通过编制模版,然后动态修改word内容以组合成新的文件。报告单、请假单、发票页等都可以使用动态生成word来解决。

笔者总结归纳出通用技术要点,尽可能降低广大开发者的使用技术门槛。

二、制作与渲染模版

(一)总体流程

1、准备数据

通过查询数据库获取需要修改的数据,或者是调用远程API接口获得数据,数据准备完毕后,进入下一步。

2、制作word模版

新建并设计出期望效果的word文档样式,包含字体、字号、段落样式布局等,先做出一个静态的word文件。

3、制作freemark模版

在新建word模版的基础上,使用freemark语法,结合已经准备填充的数据结构,将需要动态变化的内容用变量表示。

用变量替换时常见的情形时对象属性和循环。

freemark模版制作完成后,保存为ftl后缀文件。

4、渲染字符串

将数据和freemark模版组合,并且将前期制作的变量占位符替换,形成最终的word文件

(二)编码实践

按照笔者提供的流程和SDK编码实践相对比较简单。

1、引入依赖

如下依赖包含封装的工具方法,大幅降低使用门槛。

<dependency>
    <groupId>xin.altitude.cms</groupId>
    <artifactId>ucode-cms-word</artifactId>
    <version>1.5.6</version>
</dependency>

2、编码<

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您可以使用XDocReport和FreeMarker模板来生成带页数的Word文档。以下是一个示例代码: 1. 添加以下依赖项到您的项目中: ```xml <dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactId>xdocreport</artifactId> <version>1.0.6</version> </dependency> <dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactId>xdocreport-template-freemarker</artifactId> <version>1.0.6</version> </dependency> ``` 2. 创建一个FreeMarker模板,包含一个名为“page”的变量,用于显示当前页数。例如,以下是一个简单的模板: ``` <html> <head> <title>My Document</title> </head> <body> <p>Page ${page} of ${nbPages}</p> <p>This is the content of my document.</p> </body> </html> ``` 3. 在Java代码中使用XDocReport和FreeMarker模板来填充Word文档。以下是一个示例代码: ```java import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.HashMap; import java.util.Map; import org.apache.poi.xwpf.usermodel.XWPFDocument; import fr.opensagres.xdocreport.core.XDocReportException; import fr.opensagres.xdocreport.core.io.XOutputStream; import fr.opensagres.xdocreport.document.registry.XDocReportRegistry; import fr.opensagres.xdocreport.template.TemplateEngineKind; import fr.opensagres.xdocreport.template.formatter.FieldsMetadata; public class GenerateWordDoc { public static void main(String[] args) { try { // Load the template InputStream in = GenerateWordDoc.class.getResourceAsStream("template.docx"); XWPFDocument document = new XWPFDocument(in); FieldsMetadata metadata = new FieldsMetadata(); metadata.addFieldAsTextStyling("page"); XDocReportRegistry.getRegistry().register(document, metadata, TemplateEngineKind.Freemarker); // Populate the template Map<String, Object> context = new HashMap<>(); context.put("page", "${page}"); context.put("nbPages", "${nbPages}"); OutputStream out = new FileOutputStream("output.docx"); XDocReportRegistry.getRegistry().getReport(document, null).process(context, new XOutputStream(out)); // Close the streams in.close(); out.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 此代码将使用名为“template.docx”的模板创建一个新的Word文档,并将“page”变量替换为当前页数。请注意,您需要使用FieldsMetadata对象来标识文档中的字段,并将其注册到XDocReportRegistry中。最后,代码将输出生成Word文档到名为“output.docx”的文件中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值