package com.ag.secp.platform.office.word;

import freemarker.template.Configuration;
import freemarker.template.ObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/15.
 */
public class ExpFtlSimWord {
    /**
     * ftl生成word
     * @param params
     * @param path
     * @param ftlFile
     * @param osFilePath
     * @throws IOException
     */
    public static void createDoc(Map params,String path,String ftlFile,String osFilePath) throws IOException {
        Configuration configuration = new Configuration();
        configuration.setDefaultEncoding("UTF-8");
        configuration.setDirectoryForTemplateLoading(new File(path));
        Template t = null;
        try {
            t = configuration.getTemplate(ftlFile, "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        File outFile = new File(osFilePath);
        Writer out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"));
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        try {
            t.process(params, out, ObjectWrapper.BEANS_WRAPPER);
            out.close();
        } catch (TemplateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
