使用Freemark生成word

使用Freemark生成word

  1. 引入依赖
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-freemarker</artifactId>
    </dependency>
  1. 将word模板文件转换为ftl文件
    在这里插入图片描述
    点击另存为.xml格式文件
    在这里插入图片描述
    重命名为 信息表.ftl 即可
    在这里插入图片描述
  2. 将ftl文件转换为docx文件

源码

package com.cuhk.process.custom.util;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class DocUtil {
    private Configuration configuration = null;

    public DocUtil() {
        configuration = new Configuration();
        configuration.setDefaultEncoding("utf-8");
    }

    /**
     *
     * @param dataMap 入参数据
     * @param ftlPath ftl文件存放路径(不包括文件名)
     * @param ftlName ftl文件名
     * @param outfile 生成的docx文件
     * @throws UnsupportedEncodingException
     */
    public void createDoc(Map<String, Object> dataMap, String outfile,String ftlPath,String ftlName) throws UnsupportedEncodingException {
        //dataMap 要填入模本的数据文件
        //设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载,
        //这里我们的模板是放在template包下面
//        configuration.setClassForTemplateLoading(this.getClass(), ftlPath);
        Template template = null;
        try {
            //test.ftl为要装载的模板
            // 基于文件系统。 比如加载/home/user/template下的模板文件。
            configuration.setDirectoryForTemplateLoading(new File(ftlPath));
            template = configuration.getTemplate(ftlName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //输出文档路径及名称
        File outFile = new File(outfile);
        Writer out = null;
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(outFile);
            OutputStreamWriter oWriter = new OutputStreamWriter(fos, "UTF-8");
            //这个地方对流的编码不可或缺,使用main()单独调用时,应该可以,但是如果是web请求导出时导出后word文档就会打不开,并且包XML文件错误。主要是编码格式不正确,无法解析。
            //out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile)));
            out = new BufferedWriter(oWriter);
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
        try {
            template.process(dataMap, out);
            out.close();
            fos.close();
        } catch (TemplateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        String tmpFilePath = "C:"+
                File.separator+"tmp"+File.separator;
        String ftlPath ="C:"+File.separator+"ftl"+File.separator;
        Map<String, Object> dataMap =  new HashMap<>();
        long time = new Date().getTime();
        String ftlFile = "信息表.ftl";
        String outDocFile = tmpFilePath+"信息表"+time+".docx";
//        String outPdfFile = tmpFilePath+"信息表"+time +".pdf";
        // ftl由系统自动带出的数据
        dataMap.put("name","joahyan");
        dataMap.put("birthay","Oct 23th 1995");
        dataMap.put("sex","男");
        dataMap.put("age","24");
        DocUtil docUtil = new DocUtil();
        docUtil.createDoc(dataMap,outDocFile,ftlPath,ftlFile);
    }
}

生成的word文件在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值