java利用freemarker动态生成word文档及动态表格

这里写自定义目录标题

目标

根据给定word模板,动态填充指定内容,并输出为新的word文档。

准备

1.模板:

准备一份目标格式的word文档,如:word模板.docx Alt

2.pom:

引用freemarker,可以直接引用,也可以引用starterAlt

实现

处理模板

调整好格式的word模板 word模板.docx ,需要另存为 .xml 文件(打开后的xml文件内容可能有点乱,可以格式化后进行操作:在线格式化),然后将需要填充的地方设置参数,最终将 .xml 文件后缀手动改为 .ftl ,模板才算处理完整。

1.将.docx 文件另存为 .xml 文件
在这里插入图片描述
2.将 .xml 文件格式化:在线格式化:https://c.runoob.com/front-end/710/
格式化前
格式化后
3.预设参数
找到需要填充或替换的地方,以${param}的形式进行参数预设。
注意: ${} 中的参数非常重要,必须和后面java代码中 传入 的参数名称 保持一致 !且出现在模板中的预设参数,在java生成文件时,不可传null!!
如:
在这里插入图片描述
动态表格:需要选定动态生成的范围,添加list标签
通过word模板中需要填充的表格示例“内容4”,找到这一行的标签</w:tr>
在这里插入图片描述
再找到<w:tr>起始标签,添加<#list values as item>,其中 values 是后面 java 代码中生成文件时,传入的参数名。
在这里插入图片描述
再回到</w:tr>,添加</#list>闭合标签
在这里插入图片描述
4.更改后缀名:
全部参数预设完成后,保存文件,并将文件后缀改为 .ftl

java实现

        <!--生成word-->
        <!-- freemarker jar -->
<!--        <dependency>-->
<!--            <groupId>org.freemarker</groupId>-->
<!--            <artifactId>freemarker</artifactId>-->
<!--            <version>2.3.28</version>-->
<!--        </dependency>-->
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-freemarker -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
            <version>3.0.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/e-iceblue/spire.doc.free -->
        <!--为了后续解决用WPS创建的文档部分手机不能兼容,出现很多格式的问题,使用office手机可以很好的兼容-->
        <dependency>
            <groupId>e-iceblue</groupId>
            <artifactId>spire.doc.free</artifactId>
            <version>5.2.0</version>
        </dependency>
package com.example.demo.word2pdf;

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @date
 * @apiNote 生成动态文档帮助类
 */
@Data
public class WordUtil {

    /**
     * 失败错误码
     */
    public static String FILE_CREATE_FAIL = "FAIL";

    private Configuration configuration = null;

    /*
     * 模板文件存放的目录
     */
    private String baseDir;

    /*
     * 模板文件名称
     */
    private String templateFile;

    /*
     * word生成的输出目录
     */
    private String outputDir;

    public WordUtil() {
        configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
        configuration.setDefaultEncoding("UTF-8");
    }

    /*
     * 转换成word
     */
    public String createWord(Map<String, Object> dataMap) {
        configuration.setClassForTemplateLoading(this.getClass(), "");
        Template t = null;
        try {
            //得到模板文件
            configuration.setDirectoryForTemplateLoading(new File(baseDir));
            t = configuration.getTemplate(templateFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // GwUtil.getFileNo(""); 调用生成随机数的方法
//        File outFile = new File(outputDir + GwUtil.getFileNo("") + ".docx"); //导出文件
        File outFile = new File(outputDir + "\\" + Math.random() + ".docx"); //导出文件
        Writer out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile)));
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
        try {
            t.process(dataMap, out); //将填充数据填入模板文件并输出到目标文件
            return outFile.getPath();
        } catch (TemplateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return this.FILE_CREATE_FAIL;
    }

    public static void main(String[] args) throws Exception {
        // 调用word文档帮助类
        WordUtil wordUtil = new WordUtil();
        // 模板文件存放的目录
        wordUtil.setBaseDir("E:\\test2");
        // 模板文件名称
        wordUtil.setTemplateFile("word模板.ftl");
        // word生成的输出目录
        wordUtil.setOutputDir("E:\\test2");
        // 初始化数据map
        Map<String, Object> dataMap = new HashMap<>();
        // word预设参数
        dataMap.put("value1", "中文werwer");
        dataMap.put("value2", "您暗室逢灯dasdfas");
        dataMap.put("value3", "wret地方发放到");
        dataMap.put("value4", "公司hello world法规");

        // 动态表格数据
        List<Value> values = new ArrayList<>();
        values.add(new Value("内容1-1", "内容2-1", "内容3-1", "内容4-1"));
        values.add(new Value("内容1-2", "内容2-2", "内容3-2", "内容4-2"));
        values.add(new Value("内容1-3", "内容2-3", "内容3-3", "内容4-3"));
        values.add(new Value("内容1-4", "内容2-4", "内容3-4", "内容4-4"));
        dataMap.put("values", values);

        String outPath = wordUtil.createWord(dataMap);
        if (outPath.equals(FILE_CREATE_FAIL)) {
            throw new Exception("文档生成失败");
        }
        //转化后本质是doc(x)的word文档。
        Document document = new Document(outPath);
        File file = new File(outPath);
        // 判断有没有父路径,就是判断文件整个路径是否存在
        if (!file.getParentFile().exists()) {
            // 不存在就全部创建
            file.getParentFile().mkdirs();
        }
        //将本质是xml的文件转化为真正的doc(x)
        document.saveToFile(outPath, FileFormat.Docx);
        //输出生成后的文件路径
        System.out.println(outPath);
    }

    @Data
    @AllArgsConstructor
    public static class Value {
        private String value1;
        private String value2;
        private String value3;
        private String value4;
    }
}

验证

模板:
在这里插入图片描述
输出:
在这里插入图片描述

  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot_Freemarker生成Word_多个表格+两层嵌套循环; 步骤说明: 1.用Microsoft Office Word打开word原件;将文档中需要动态生成的内容,替换为属性名 ${name} 2.另存为,选择保存类型Word 2003 XML 文档(*.xml) 3.用Firstobject free XML editor打开文件,选择Tools下的Indent【或者按快捷键F8】格式化文件内容。左边是文档结构,右边是文档内容; 4. 文档生成后有时需要手动修改,查找第一步中设置的属性名,可能会产生类似${n.....ame}类似的样子,我们将将名字中间的标签删掉,恢复为${name} 5. word模板中有表格,需要循环的位置, 用 标签将第二对 标签(即除表头的w:tr标签后的一对)包围起来 同时表格内的属性例如${name},在这里需要修改为${user.name} (userList是集合在dataMap中的key, user是集合中的每个元素, 类似), 如图: PLUS:若表格之外还有嵌套的循环,也需要用,注意这里的标签不要和某对其他标签交叉,不可以出现这种 6. 标识替换完之后,另存为.ftl后缀文件即可。 代码里是相对有一丢丢复杂的,两层嵌套循环; 总(dataMap) deptName 部门名 list(Table)表的集合 table1(map) table-名字 ${map.table} tableName-中文名 ${map.tableName} columnCount-字段数 ${map.columnCount} recordCount-记录数 ${map.recordCount} listA-List--表格1 map.listA column Model属性——字段名 ${model.column} columnName Model属性——字段中文名 ${model.column} rate Model属性——字段占比 ${model.rate} nullValueCount Model属性——字段空值数 ${model.nullValueCount} listB-List--表格2 map.listB …… listC-List--表格3 map.listC …… table2 table-名字 ${map.table} tableName-中文名 ${map.tableName} columnCount-字段数 ${map.columnCount} recordCount-记录数 ${map.recordCount} listA-List--表格1 map.listA column Model属性——字段名 ${model.column} columnName Model属性——字段中文名 ${model.column} rate Model属性——字段占比 ${model.rate} nullValueCount Model属性——字段空值数 ${model.nullValueCount} listB-List--表格2 map.listB …… listC-List--表格3 map.listC …… table3 ……

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值