当前位置:网站首页>freemark 使用ftl文件 生成word

freemark 使用ftl文件 生成word

2022-06-23 15:07:00 softwareDragon

    <dependency>
      <groupId>org.freemarker</groupId>
      <artifactId>freemarker</artifactId>
      <version>2.3.23</version>
    </dependency>

import freemarker.template.TemplateException;
import java.io.*;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
public class AA {

    //dataMap为数据模型 ftlName为bb这个目录下的ftl文件
    public static byte[] createDocForFtl(Map<String, Object> dataMap,String ftlName)     
    throws IOException {
        Configuration configuration = new Configuration();
        configuration.setDefaultEncoding("utf-8");
        //需要导出模板的包路径
        configuration.setClassForTemplateLoading(AA.class, "/bb");
        Template t = null;
        try {
            t = configuration.getTemplate(ftlName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Writer out = new StringWriter();
        //将模板和数据模型合并生成文件
        try {
            //生成文件
            t.process(dataMap, out);
            return out.toString().getBytes();
        } catch (TemplateException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }
        return null;
    }
}

原网站

版权声明
本文为[softwareDragon]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_33348135/article/details/125004634