栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

生成word进阶(动态word模板,可以调整顺序-类似前端组件化)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

生成word进阶(动态word模板,可以调整顺序-类似前端组件化)

描述:

freemarker生成的 本质上 读文件(成字符串),然后替换特定标记的字符串。。

思路:那么,掐头去尾,一个空白页(或者放个标题)作为父组件,内部放一个用来替换的标记。

然后把所有子组件,挨个替换内部的字符串,按需要的顺序拼接起来,再替换到父组件上,就实现了这个排序的功能。。

  • 总结:子组件挨个替换,拼接起来,替换进父组件(可能写的方法不是很好,只是表达了这么一个实现思路)
1. 工具类
package com.hnj.yq.common.utils;

import com.hnj.yq.common.model.doc.WriteDocChildDTO;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DocUtils {

    public static void generateWord(List childParams, String templatePath, String templateName, String outputPath) throws IOException, TemplateException {
        //.文件地址的目录  是否存在,不存在新建目录
        File dest = new File(outputPath);
        // 检测是否存在目录
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();// 新建文件夹
        }
        Configuration configuration = new Configuration();
        configuration.setDefaultEncoding("utf-8");
        //外层模板:
        Map map = new HashMap<>();
        //先制作内部模板,最后替换到外部模板上。
        if (true) {
            //内部模板和数据:
            StringBuilder replace = new StringBuilder();
            for (WriteDocChildDTO childParam : childParams) {
                String childTemplatePath = childParam.getTemplatePath();
                String childrenTemplateName = childParam.getTemplateName();
                StringWriter result = new StringWriter();
                String childTemplate = readFile(childTemplatePath + childrenTemplateName);
                Template t = new Template(childrenTemplateName, new StringReader(childTemplate), new Configuration());
                t.process(childParam.getData(), result);//替换子模板
                String str = result.toString();
                replace.append(str);
            }
            String templateValue = replace.toString();
            map.put("template", templateValue);
        }

        //方式3 自己读文件成字符串,再创建模板template
        String str = readFile(templatePath + templateName);
        Template template = new Template(templateName, new StringReader(str), configuration);

        Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputPath), StandardCharsets.UTF_8), 10240);
        template.process(map, out);
        out.close();

    }
    private static String readFile(String filePath) {
        String str = "";
        File file = new File(filePath);
        try {
            FileInputStream in = new FileInputStream(file);
            int size = in.available();
            byte[] buffer = new byte[size];
            int read = in.read(buffer);
            in.close();
            str = new String(buffer, StandardCharsets.UTF_8);
        } catch (IOException ignore) {

        }
        return str;
    }


}

2. 使用
package com.hnj.yq.common.utils;

import com.hnj.yq.common.model.doc.WriteDocChildDTO;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DocUtils {

    public static void main(String[] args) {
        List childParams = new ArrayList<>();

        //数据1
        WriteDocChildDTO dto1 = new WriteDocChildDTO();
        dto1.setTemplatePath("C:\uploadFile\yq-server\template\");
        dto1.setTemplateName("1652339396055.ftl");
        Map map1 = new HashMap<>();
        map1.put("name", 3);
        dto1.setData(map1);

        childParams.add(dto1);

        //数据2
        WriteDocChildDTO dto2 = new WriteDocChildDTO();
        dto2.setTemplatePath("C:\uploadFile\yq-server\template\");
        dto2.setTemplateName("1652339396055.ftl");
        Map map2 = new HashMap<>();
        map2.put("name", 2);
        dto2.setData(map2);
        childParams.add(dto2);

        //数据3
        WriteDocChildDTO dto3 = new WriteDocChildDTO();
        dto3.setTemplatePath("C:\uploadFile\yq-server\template\");
        dto3.setTemplateName("1652339396055.ftl");
        Map map3 = new HashMap<>();
        map3.put("name", 4);
        dto3.setData(map3);
        childParams.add(dto3);

        try {
            generateWord(childParams, "C:\uploadFile\yq-server\template\", "1652339363878.ftl", "c:\" + System.currentTimeMillis() + ".doc");
        } catch (IOException | TemplateException e) {
            System.out.println(e);
        }
    }


}


WriteDocParam

package com.hnj.yq.common.model.doc;

import lombok.Data;

import java.io.Serializable;
import java.util.Map;

@Data
public class WriteDocChildDTO implements Serializable {

    private String templatePath;//模板路径

    private String templateName;//模板文件名
	
    private Map data;//参数
}

3. 模板示例 3.1 父模板

1652339363878.ftl




        Administrator
        Administrator
        2022-05-12T07:04:26Z
        2022-05-12T07:05:11Z
        1
        8
        8
        0
        0
        8
        14
    
    
        2052-11.1.0.11636
        C7AA28CA12AF465C94517D629B9812AA
    
    
        
        
            
            
            
            
            
        
        
            
            
            
            
            
        
        
            
            
            
            
            
        
        
            
            
            
            
            
        
        
            
            
            
            
            
        
        
            
            
            
            
            
        
        
            
            
            
            
            
        
        
            
            
            
            
            
        
    
    
        
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
        
        
            
            
                
                
            
            
                
                
                
                
                
            
        
        
            
            
        
        
            
            
            
                
                
                    
                    
                    
                    
                
            
        
    
    
        
        
            
        
    
    
        
        
        
        
        
        
        
        
        
        
        
        
            
            
            
            
            
            
            
            
            
            
            
        
    
    
        
            
                
                    
                        
                        
                    
                
                
                    
                        
                        
                    
                    标题
                
            
            
                
                    
                        
                        
                    
                
            
            
                
                    
                        
                        
                    
                
            
            ${template}
            
                
                
                
                
            
        
    
3.2 子模板

1652339396055.ftl


    
        
            
            
        
    
    
        
            
            
        
        ${name}
    

PS :如何生成,以及如何使用模板,看我上一篇文章
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/878324.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号