package com.hoperun.ccca.com mon.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import Java.io.output streamwriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.List;
import java.util.Map;
importorg.Apache.com mons.lang.string utils;
import com.hoperun.ccca.com mon.util.param.word params;
导入freemarker.cache.filetemplateloader;
导入freemarker.cache.multitemplateloader;
导入freemarker.cache.template loader;
导入freemarker.template.configuration;
import freemarker.template.defaultobjectwrapper;
导入freemarker.template.template;
导入freemarker.template.template exception;
公共类自由标记器utils {
私有状态配置文件=空值;
私密性staticstringdefault _ encoding=' utf-8 ';
静态{
cfg=new Configuration (;
CFG.set default编码(default _ encoding;
}
publicstaticstringbuildstr (字符串时间表,列表参数)。
字符串结果=null;
字符串写入器SW=null;
try {
sw=new StringWriter (;
//加载模板文件
if(stringutils.isblank(tempdir ) )。
throw new RuntimeException ('模板文件路径不能为空);
}
加载文件模板(tempdir;
把word拼起来
for(wordparamsparam:params ) {
appendftl(SW,param );
}
转移到字符串
result=sw.toString (;
}catch(exceptionex ) {
throwdealexception(ex;
} finally {
try {
If (软件!=空) {
sw.flush (;
sw.close (;
}
}catch(exceptionex ) {
//暂时不处理
} finally {
sw=null;
}
}
返回结果;
}
//*
导出excel文件
* @param templateDir模板路径
* @param templateName模板名称
* @param excelPath导出的文件路径
* @ param数据
* @throws IOException
* @ throws模板执行
*/
publicstaticvoidparse (stringtemplatedir、String templateName、String excelPath、Map data ) {
//初始化流
输出流写入器=null;
//初始化配置
配置CFG=new配置(;
//设置默认编码
格式为UTF-8cfg.setDefaultEncoding(DEFAULT_ENCODING);
// 全局数字格式
cfg.setNumberFormat("0.00");
try {
// 设置模板文件位置
cfg.setDirectoryForTemplateLoading(new File(templateDir));
cfg.setObjectWrapper(new DefaultObjectWrapper());
// 加载模板
Template template = cfg.getTemplate(templateName, DEFAULT_ENCODING);
// 填充数据至Excel
writer = new OutputStreamWriter(new FileOutputStream(excelPath), DEFAULT_ENCODING);
template.process(data, writer);
writer.flush();
} catch (IOException | TemplateException e) {
e.printStackTrace();
} finally {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 创建word文件
* @param destPath 生成文件全路径
* @param tempDir ftl文件夹路径
* @param params 文件名和文件替换参数
*/
public static void buildWord(String destPath, String tempDir, List params) {
PrintWriter pw = null;
try {
// 创建目标文件流
if (StringUtils.isBlank(destPath)) {
throw new RuntimeException("生成文件路径不能为空");
}
// 创建文件路径
File file = new File(destPath);
if (!file.getParentFile().exists()) {
if(!file.getParentFile().mkdirs()) {
throw new RuntimeException("创建文件路径失败");
}
}
// 创建文件流
pw = new PrintWriter(new File(destPath), DEFAULT_ENCODING);
// 加载模板文件
if (StringUtils.isBlank(tempDir)) {
throw new RuntimeException("模板文件路径不能为空");
}
loadFileTemplate(tempDir);
// 拼接word
for (WordParams param : params) {
appendFtl(pw, param);
}
} catch (Exception ex) {
throw dealException(ex);
} finally {
try {
if (pw != null) {
pw.flush();
pw.close();
}
} catch (Exception ex) {
// 暂不处理
} finally {
pw = null;
}
}
}
/**
* 拼接模板文件
* @param pw
* @param param
*/
private static void appendFtl(Writer pw, WordParams param) {
try {
// 读取模板文件
Template template = cfg.getTemplate(param.getFtlName());
// 写入文件
template.process(param.getFtlValue(), pw);
} catch (Exception ex) {
throw dealException(ex);
}
}
/**
* 加载模板文件
* @param baseDir
*/
private static void loadFileTemplate(String tempDir) {
try {
// 加载模板文件
FileTemplateLoader loader = new FileTemplateLoader(new File(tempDir));
TemplateLoader[] loaders = { loader };
MultiTemplateLoader mtl = new MultiTemplateLoader(loaders);
// 读取模板
cfg.setTemplateLoader(mtl);
cfg.setDefaultEncoding(DEFAULT_ENCODING);
} catch (Exception ex) {
throw dealException(ex);
}
}
/**
* 调用异常处理
* @param ex
* @return
*/
private static RuntimeException dealException(Exception ex) {
if (ex == null) {
return null;
// } else if (ex instanceof AxisFault) {
// if (ex.getCause() instanceof SocketTimeoutException) { // 调用超时
// return new ServiceInterruptedException(ex.getCause());
// } else if (ex.getCause() instanceof ConnectTimeoutException) { // 连接超时
// // 暂不作特殊处理
// return new RuntimeException(ex);
// } else {
// return new RuntimeException(ex);
// }
} else {
return new RuntimeException(ex);
}
}
}