package com.hoperun.ccca.common.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import com.hoperun.ccca.common.util.param.WordParams;
import freemarker.cache.FileTemplateLoader;
import freemarker.cache.MultiTemplateLoader;
import freemarker.cache.TemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class FreeMarkerUtils {
private static Configuration cfg = null;
private static String DEFAULT_ENCODING = "UTF-8";
static {
cfg = new Configuration();
cfg.setDefaultEncoding(DEFAULT_ENCODING);
}
public static String buildStr(String tempDir, List<WordParams> params) {
String result = null;
StringWriter sw = null;
try {
sw = new StringWriter();
// 加载模板文件
if (StringUtils.isBlank(tempDir)) {
throw new RuntimeException("模板文件路径不能为空");
}
loadFileTemplate(tempDir);
// 拼接word
for (WordParams param : params) {
appendFtl(sw, param);
}
// 转string
result = sw.toString();
} catch (Exception ex) {
throw dealException(ex);
} finally {
try {
if (sw != null) {
sw.flush();
sw.close();
}
} catch (Exception ex) {
// 暂不处理
} finally {
sw = null;
}
}
return result;
}
/**
* 导出excel文件
* @param templateDir 模板路径
* @param templateName 模板名称
* @param excelPath 导出的文件路径
* @param data 数据
* @throws IOException
* @throws TemplateException
*/
public static void parse(String templateDir, String templateName, String excelPath, Map<String, Object> data) {
// 初始化流
OutputStreamWriter writer = null;
// 初始化配置
Configuration cfg = new Configuration();
// 设置默认编码格式为UTF-8
cfg.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<WordParams> 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);
}
}
}