派生自 projectDept/qhighschool

yn147
2023-11-23 bccada7cbf7eea3c37c0243d95426d1a29d9121f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.qxueyou.scc.base.util;
 
import java.io.IOException;
 
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;
 
public final class WordExportUtils {
    
    public Configuration configure;
    
    public WordExportUtils(){
        configure = new Configuration(Configuration.VERSION_2_3_21);
        configure.setDefaultEncoding("utf-8");
    }
    /**
     * 根据Doc模板生成word文件
     * @param dataMap 需要填入模板的数据
     * @param downloadType 文件名称
     */
    public Template createDoc(String downloadType){
        try {
            //设置模板装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载。
            //加载模板文件,放在testDoc下
            configure.setClassForTemplateLoading(this.getClass(), "");
            //设置异常处理器
            configure.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
            
            return configure.getTemplate(downloadType);//定义Template对象,注意模板类型名字与downloadType要一致
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}