java通过模板导出docx文档

本文档详细介绍了如何在Java中利用FreeMarker模板库将数据填充到docx文档中,通过生成XML文件并替换document.xml来实现模板的动态生成。主要步骤包括设置模板目录、创建配置、处理数据、生成带数据的XML以及最终合并成docx文件。
摘要由CSDN通过智能技术生成

@

java通过模板导出docx文档


二、使用步骤

代码如下(示例):

import freemarker.template.Configuration;
import freemarker.template.Template;
 
import java.io.*;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
 
public class Main {
 
    public static void main(String[] args) {
        // 路径
        String templatepath = "D:\\Projects\\lib\\generateDocx";
        String docxname = "demo.docx";
        String xmlname = "demo.xml";
        String tmpxmlpath = "D:\\Projects\\lib\\generateDocx\\complete.xml";
        String targetpath = "D:\\Projects\\lib\\generateDocx\\complete.docx";
        // 数据
        Map<String,Object> data = new HashMap();
        data.put("words","这里是文字");
        // 生成文档
        try {
            generate(templatepath, docxname, xmlname, tmpxmlpath, targetpath, data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * @Description 根据参数生成docx合同文档
     * @author belle.wang
     * @param templatepath 模板所在文件夹
     * @param docxname docx格式模板文件名(不带路径)
     * @param xmlname xml格式模板,有freemaker标记(不带路径)
     * @param tmpxmlpath 临时xml文件路径
     * @param targetPath 目标路径
     * @param param 待填充数据
     * @return
     * @throws Exception
     */
    private static boolean generate(String templatepath, String docxname, String xmlname,
                                    String tmpxmlpath, String targetPath, Map<String, Object> param) throws Exception {
        Configuration cfg = new Configuration();
        cfg.setDirectoryForTemplateLoading(new File(templatepath));
        Template template = cfg.getTemplate(xmlname);
        template.setOutputEncoding("UTF-8");
        Writer out = new FileWriter(new File(tmpxmlpath));
        // 数据放到模板xml里面,生成带数据的xml
        template.process(param, out);
        if (out != null) {
            out.close();
        }
        // 带数据的xml生成docx
        File file = new File(tmpxmlpath);
        File docxFile = new File(templatepath + "/" + docxname);
        ZipFile zipFile = new ZipFile(docxFile);
        Enumeration<? extends ZipEntry> zipEntrys = zipFile.entries();
        ZipOutputStream zipout = new ZipOutputStream(new FileOutputStream(targetPath));
        int len = -1;
        byte[] buffer = new byte[1024];
        while (zipEntrys.hasMoreElements()) {
            ZipEntry next = zipEntrys.nextElement();
            InputStream is = zipFile.getInputStream(next);
            // 把输入流的文件传到输出流中 如果是word/document.xml由我们输入
            zipout.putNextEntry(new ZipEntry(next.toString()));
            if ("word/document.xml".equals(next.toString())) {
                InputStream in = new FileInputStream(file);
                while ((len = in.read(buffer)) != -1) {
                    zipout.write(buffer, 0, len);
                }
                in.close();
            } else {
                while ((len = is.read(buffer)) != -1) {
                    zipout.write(buffer, 0, len);
                }
                is.close();
            }
        }
        zipout.close();
        return true;
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

qq_39493446

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

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

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

打赏作者

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

抵扣说明:

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

余额充值