springboot 使用XDocReport和FreeMarker模板生成带页数和总页数的word

时间: 2024-02-22 07:57:07 浏览: 26
您可以使用XDocReport和FreeMarker模板来生成带页数和总页数的Word文档。以下是一个示例代码: 1. 添加以下依赖项到您的Spring Boot项目中: ```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”和“nbPages”的变量,分别用于显示当前页数和总页数。例如,以下是一个简单的模板: ``` <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. 创建一个Spring Boot服务类,并在其中注入XDocReportRegistry和TemplateEngine对象。以下是一个示例代码: ```java import java.io.InputStream; import java.io.OutputStream; import java.util.HashMap; import java.util.Map; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; 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.TemplateEngine; import fr.opensagres.xdocreport.template.TemplateEngineKind; import fr.opensagres.xdocreport.template.formatter.FieldsMetadata; @Service public class WordService { @Autowired private XDocReportRegistry registry; @Autowired private TemplateEngine templateEngine; public void generateWordDoc(OutputStream out) { try { // Load the template InputStream in = WordService.class.getResourceAsStream("template.docx"); XWPFDocument document = new XWPFDocument(in); FieldsMetadata metadata = new FieldsMetadata(); metadata.addFieldAsTextStyling("page"); metadata.addFieldAsTextStyling("nbPages"); registry.register(document, metadata, TemplateEngineKind.Freemarker); // Populate the template Map<String, Object> context = new HashMap<>(); context.put("page", "${page}"); context.put("nbPages", "${nbPages}"); templateEngine.process(context, registry.getReport(document, null), out); // Close the streams in.close(); out.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 此代码将创建一个Spring Boot服务类,并使用注入的XDocReportRegistry和TemplateEngine对象来生成Word文档。请注意,您需要使用FieldsMetadata对象来标识文档中的字段,并将其注册到XDocReportRegistry中。最后,代码将输出生成的Word文档到输出流中。

相关推荐

最新推荐

recommend-type

基于Freemarker和xml实现Java导出word

主要介绍了基于Freemarker和xml实现Java导出word,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

java生成pdf(利用dom4j、freemarker生成固定模板格式的pdf文件)

背景:跟着我的步骤一步一步...需要用画笔去花模板,很繁琐! Apache PDFBox,生成、合并PDF文档; docx4j,生成docx、pptx、xlsx文档,支持转换为PDF格式。 本文使用最后一种,思路如下图: 步骤 1.首先将你要生成的
recommend-type

Springboot vue导出功能实现代码

主要介绍了Springboot vue导出功能实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

模板:velocity和freemarker的比较

模板:velocity和freemarker的比较模板:velocity和freemarker的比较
recommend-type

yolov5-face-landmarks-opencv

yolov5检测人脸和关键点,只依赖opencv库就可以运行,程序包含C++和Python两个版本的。 本套程序根据https://github.com/deepcam-cn/yolov5-face 里提供的训练模型.pt文件。转换成onnx文件, 然后使用opencv读取onnx文件做前向推理,onnx文件从百度云盘下载,下载 链接:https://pan.baidu.com/s/14qvEOB90CcVJwVC5jNcu3A 提取码:duwc 下载完成后,onnx文件存放目录里,C++版本的主程序是main_yolo.cpp,Python版本的主程序是main.py 。此外,还有一个main_export_onnx.py文件,它是读取pytorch训练模型.pt文件生成onnx文件的。 如果你想重新生成onnx文件,不能直接在该目录下运行的,你需要把文件拷贝到https://github.com/deepcam-cn/yolov5-face 的主目录里运行,就可以生成onnx文件。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。