读word模板批量生成制式文件

1、Maven依赖

  <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.template</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.freemarker</artifactId>
    <version>2.0.2</version>
  </dependency>

2、.docx或.doc格式的word模板准备

  1. 创建.docx文件,编写内容

image.png

  1. 添加编辑域

按Ctrl+F9,创建编辑域
image.png
右击,选择编辑域
image.png
选择邮件合并,修改域代码要注意域代码的格式为:MERGEFIELD ${name}name为需要填充的内容。
image.png
        依次类推,填上所有的编辑域,调整文档格式等,就完成了word文档准备。
        之后需要代码读word模板转数据流,进行代码域的变量批量替换,重新写文件就完成的word文件生成工作。

3、读word模板,批量替换代码域,生成文件,demo

package com.dongzi.utils.word;

import fr.opensagres.xdocreport.core.XDocReportException;
import fr.opensagres.xdocreport.document.IXDocReport;
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
import fr.opensagres.xdocreport.template.IContext;
import fr.opensagres.xdocreport.template.TemplateEngineKind;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 读word模板,生成数据
 */
public class ReadWordTemplate {
	
    public static void main(String[] args) throws IOException, XDocReportException {
        // 从resources/template中获取word模板数据
        IXDocReport ixDocReport = readWord("info.docx");
        IContext context = ixDocReport.createContext();
        // set value
        // putTemplateValue_1(context);
        // putTemplateValue_2(context);
        putTemplateValue_3(context);


        // =============

        FileOutputStream out = new FileOutputStream("D:/temp/docx模板输出.docx");
        ixDocReport.process(context, out);
        out.flush();
        out.close();
    }

    public static IXDocReport readWord(String fileName) throws IOException, XDocReportException {
        // 读模板的方式
        // 方式1:通过URL加载
//        URL url = ClassLoader.getSystemClassLoader().getResource("template/" + fileName);
//        assert url != null;
//        InputStream in_1 = url.openStream();

        // 方式2:系统资源转数据流
        InputStream in_2 = ClassLoader.getSystemResourceAsStream("template/" + fileName);

        // 方式3:spring的类加载器,获取资源
//        ClassPathResource pathResource = new ClassPathResource("template/" + fileName);
//        InputStream in_3 = pathResource.getInputStream();

        return XDocReportRegistry.getRegistry().loadReport(in_2, TemplateEngineKind.Freemarker);
    }

    // 设置docx模板值
    public static void putTemplateValue_1(IContext context) {
        // docx文档模板读取,必须要预先再模板里面设置文本域,Ctrl+F9
        /*
            1. Ctrl+F9,打开编辑域
            2. 域选择“邮件合并”,域代码为:MERGEFIELD ${yourAddress}
            3. 页面展示的格式为:«${yourAddress}»,模板可替换
         */
        context.put("yourName", "孙悟空");
        context.put("yourAge", "500");
        context.put("yourAddress", "花果山水帘洞");
    }

    public static void putTemplateValue_2(IContext context) {
        // 生成数据
        Map<String, Object> mapValues = new HashMap<>();
        mapValues.put("yourName", "齐天大圣");
        mapValues.put("yourAge", 36000);
        mapValues.put("yourAddress", "花果山水帘洞");
        //
        context.putMap(mapValues);
    }

    public static void putTemplateValue_3(IContext context) {
        // 生成数据
        Map<String, Object> mapValues = new HashMap<>();
        mapValues.put("yourName", "派大星");
        mapValues.put("yourAge", 36000);
        mapValues.put("yourAddress", "黄土高坡");

        for (int i = 1; i <= 10; i++) {
            mapValues.put("index_" + i, i);
            mapValues.put("value_" + i, i + 2);
        }
        //
        context.putMap(mapValues);
    }

}

4、结果展示

image.png
至此,整个模板读取生成过程全部结束。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以使用JavaScript来操作Word模板生成Word文件。一种常见的方法是使用第三方库,如`docxtemplater`或`officegen`来处理Word文档的生成。 首先,你需要安装所选库的依赖项。以`docxtemplater`为例,可以使用以下命令安装: ``` npm install docxtemplater ``` 然后,你可以使用以下代码示例来操作Word模板生成Word文件: ```javascript const fs = require('fs'); const Docxtemplater = require('docxtemplater'); // Word模板文件 const templateContent = fs.readFileSync('template.docx', 'binary'); // 创建一个新的Docxtemplater实例 const doc = new Docxtemplater(); // 加载模板内容 doc.loadZip(new JSZip(templateContent)); // 设置要替换的变量值 const data = { name: 'John Doe', age: 30, address: '123 Main Street', }; // 将变量值应用于模板 doc.setData(data); // 渲染模板内容 doc.render(); // 生成Word文件 const output = doc.getZip().generate({ type: 'nodebuffer' }); // 将文件保存到磁盘 fs.writeFileSync('output.docx', output); ``` 在上述示例中,我们首先使用`fs`模块Word模板文件的内容。然后,我们创建一个`Docxtemplater`实例,并加载模板内容。接下来,我们设置要替换的变量值,并将其应用于模板。最后,我们渲染模板内容,并将生成Word文件保存到磁盘。 请注意,这只是一个简单的示例,你可以根据自己的需求进行定制和扩展。另外,还可以使用其他类似的库来完成类似的任务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小白说(๑• . •๑)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值