package com.ruoyi.powerdistribution.util;

import freemarker.core.OutputFormat;
import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.util.Map;

public class GenerateWordUtil {

    /**
     * 生成word文件
     * @param dataMap word中需要展示的动态数据，用map集合来保存
     * @param templateName word模板名称，例如：test.ftl
     */
    @SuppressWarnings("unchecked")
    public static void generateWord(Map dataMap, String templateName, OutputStreamWriter out){
        try {
            //创建配置实例
            Configuration configuration = new Configuration(Configuration.VERSION_2_3_27);
            //设置编码
            configuration.setDefaultEncoding("utf-8");
            //ftl模板文件
            configuration.setClassForTemplateLoading(GenerateWordUtil.class,"/ftl");
            //获取模板
            Template template = configuration.getTemplate(templateName,"utf-8");
            template.process(dataMap, out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}