派生自 projectDept/qhighschool

胡仁荣
2022-10-31 fe7381d6e8ec1f427408de0297ac7f41533202f6
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;
    }
}