使用FreeMarker自定义生成word文档

使用FreeMarker自定义生成word文档

最终生成word文档如下:
在这里插入图片描述

实现思路:
  1. 按照要生成的文档模板格式,创建一个新的word(doc)文档,将其调整成所需格式,然后处理其中需要动态填充的数据,如下:
    在这里插入图片描述
  2. 将该word文档另存为Word 2003 XML 文档(*.xml)格式,然后将其后缀改为.ftl
  3. 此时该.ftl文件里的内容格式可能不规范,规范其内容,例如:”${map.projectName}“中间可能被其他字符给分隔。所以要把中间的无用字符给删掉;
  4. 最后生成的文件可能长这样:
    在这里插入图片描述
功能实现:

模板已经写好,现在就是后端代码实现了。

	/**
     * 生成word
     * @param projectInformation	该课题信息对象
     * @param dir	要生成的word的存储路径
     * @return
     * @throws Exception
     */
    public static String exportWord(ProjectInformation projectInformation, String dir){

        File file = new File(dir);
        if (!file.exists()){
            file.mkdirs();
        }
        Configuration conf = new Configuration();
        conf.setDefaultEncoding("UTF-8");
        //加载模板文件(模板的路径)
        conf.setDirectoryForTemplateLoading(new File(dir));
        // 加载模板
        String separator = File.separator;
        Template template = conf.getTemplate("templatePrint.ftl");
        template.setEncoding("UTF-8");
        // 定义数据
        Map root = new HashMap();

        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotBlank(projectInformation.getName())){
            map.put("projectName",projectInformation.getName());
        }else {
            map.put("projectName","");
        }
        if (StringUtils.isNotBlank(projectInformation.getName())){
            map.put("projectNumber", projectInformation.getNumber());
        }else {
            map.put("projectNumber","");
        }
        if (StringUtils.isNotBlank(projectInformation.getName())){
            map.put("serialNumber",projectInformation.getSerial());
        }else {
            map.put("serialNumber","");
        }

        SimpleDateFormat sf = new SimpleDateFormat("yyyy/MM/dd");
        String currentTime = sf.format(new Date());
        map.put("currentTime",currentTime);
        root.put("map",map);

        // 定义输出
        SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd");//设置日期格式
        String date = f.format(new Date(System.currentTimeMillis()));
        Random random = new Random();
        int rannum = (int) (random.nextDouble() * (99999 - 10000 + 1)) + 10000;// 获取5位随机数
        String fileName = date+rannum+"标签.doc";
        //判断文件夹是否存在,不存在则创建
        File fileDir = new File(HtmlUtil.getAppDir()+separator+"Template");
        if (!fileDir.exists()){
            fileDir.mkdirs();
        }
        Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(HtmlUtil.getAppDir()+separator+"Template" + separator+fileName),"UTF-8"));
        template.process(root, out);
        out.flush();
        out.close();
        return fileName;
    }    
    
##### 补充:
如果要生成的文档里面包含表格,怎么循环放置数据?
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值