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;
|
}
|
}
|