当前位置:网站首页>Freemark uses FTL files to generate word

Freemark uses FTL files to generate word

2022-06-23 16:51: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 For the data model  ftlName by bb In this directory ftl file 
    public static byte[] createDocForFtl(Map<String, Object> dataMap,String ftlName)     
    throws IOException {
        Configuration configuration = new Configuration();
        configuration.setDefaultEncoding("utf-8");
        // The package path where the template needs to be exported 
        configuration.setClassForTemplateLoading(AA.class, "/bb");
        Template t = null;
        try {
            t = configuration.getTemplate(ftlName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Writer out = new StringWriter();
        // Merge the template and data model into a file 
        try {
            // Generate the file 
            t.process(dataMap, out);
            return out.toString().getBytes();
        } catch (TemplateException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }
        return null;
    }
}

原网站

版权声明
本文为[softwareDragon]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206231506561388.html

随机推荐