Java实现根据模板下载Word文档功能 XDocReport +FreeMarker

依赖

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

代码

    public void downloadDocx(HttpServletResponse response) {
        try (
                InputStream ins = Files.newInputStream(Paths.get(docxPath));
                OutputStream out = response.getOutputStream()
        ) {
            IXDocReport report = XDocReportRegistry.getRegistry().loadReport(ins, TemplateEngineKind.Freemarker);
            IContext context = report.createContext();
            Map<String, Object> map = context.getContextMap();
            map.put("name","张三");
            map.put("salary",1500)
            Person persion1 = new Person();
            persion1.setName("张三");
            persion1.setSalary(1500);
            Person persion2 = new Person();
            persion2.setName("李四");
            persion2.setSalary(2000);
            List<Person> list = new ArrayList<>();
            list.add(persion1);
            list.add(persion2);
            map.put("person",list);

            FieldsMetadata fm = report.createFieldsMetadata();
            fm.load("person", Person.class, true);
            report.setFieldsMetadata(fm);

            String fileName = "result.docx";
            response.setCharacterEncoding(UTF_8);
            response.setContentType("application/msword");
            response.addHeader("Content-Disposition", "attachment;filename=".concat(String.valueOf(URLEncoder.encode(fileName, UTF_8))));
            report.process(context, out);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Word

结果

参考 https://blog.csdn.net/qq_55656404/article/details/125434007

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您可以使用XDocReportFreeMarker模板来生成带页数的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代码中使用XDocReportFreeMarker模板来填充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、付费专栏及课程。

余额充值