FreeMarker导出指定模板
ftl模板标签
去掉字体加粗
<w:b w:val="off" />
字体变黑体
<w:color w:val="#000000" />
格式化时间
${date?string("yyyy-MM-dd HH:mm")!}
代码中中实现换行
<w:br/>
代码中(行首)增加空格(多加几个可以效果)
     
工具类:(导出图片没有测试)
package com.efiles.common.util;
import freemarker.template.*;
import org.apache.commons.lang3.StringUtils;
import sun.misc.BASE64Encoder;
import java.io.*;
import java.net.URLEncoder;
import java.util.*;
import org.apache.log4j.Logger;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* FreeMarkerUtil 工具类
*/
public class FreeMarkerUtil {
//设置编码
private static final String ENCODING = "UTF-8";
//指定Configuration版本,基于依赖的版本
private static Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);
//初始化cfg
static {
//设置模板所在文件夹
cfg.setClassForTemplateLoading(FreeMarkerUtil.class, "/com/efiles/templates/");
// setEncoding这个方法一定要设置国家及其编码,不然在ftl中的中文在生成html后会变成乱码
cfg.setEncoding(Locale.getDefault(), ENCODING);
// 设置对象的包装器
cfg.setObjectWrapper(new DefaultObjectWrapper(Configuration.VERSION_2_3_28));
// 设置异常处理器,这样的话就可以${a.b.c.d}即使没有属性也不会出错
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
}
//获取模板对象
public static Template getTemplate(String templateFileName) throws IOException {
return cfg.getTemplate(templateFileName, ENCODING);
}
/**
* 据数据及模板生成文件
* @param data Map的数据结果集
* @param templateFileName ftl模版文件名
* @param fileTitle .doc文件名
* @param
*/
public static void ConverWord(Map data, String templateFileName, String fileTitle,
HttpServletResponse response, HttpServletRequest request) {
Writer out = null;
try {
// 获取模板,并设置编码方式,这个编码必须要与页面中的编码格式一致
Template template = getTemplate(templateFileName);
setFileDownloadHeader(request,response,fileTitle);
response.setContentType("application/msword");
ServletOutputStream fos = response.getOutputStream();
OutputStreamWriter bufWrite = new OutputStreamWriter(fos, ENCODING);
//BufferedWriter bufWrite = new BufferedWriter(new OutputStreamWriter(fos,ENCODING));
out = new BufferedWriter(bufWrite);
// 处理模版
template.process(data, out);
out.flush();
out.close();
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}catch (TemplateException e) {
e.printStackTrace();
}
}
/**
*根据当前用户的浏览器不同,对文件的名字进行不同的编码设置,从而解决不同浏览器下文件名中文乱码问题
*/
public static void setFileDownloadHeader(HttpServletRequest request, HttpServletResponse response, String fileTitle) {
final String userAgent = request.getHeader("USER-AGENT");
try {
String finalFileName = null;
if (StringUtils.contains(userAgent, "MSIE")) {
// IE浏览器
finalFileName = URLEncoder.encode(fileTitle, "UTF8");
} else if (StringUtils.contains(userAgent, "Mozilla")) {
// google,火狐浏览器
finalFileName = new String(fileTitle.getBytes(), "ISO8859-1");
} else {
//其他浏览器
finalFileName = URLEncoder.encode(fileTitle, "UTF8");
}
//这里设置一下让浏览器弹出下载提示框,而不是直接在浏览器中打开
response.setHeader("Content-Disposition", "attachment; filename=\"" + finalFileName + "\"");
} catch (Exception e) {
e.printStackTrace();
}
}
//获得图片的base64码
public static String getImageBase(String src) throws Exception {
if (src == null || src == "") {
return "";
}
File file = new File(src);
if (!file.exists()) {
return "";
}
InputStream in = null;
byte[] data = null;
try {
in = new FileInputStream(file);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);
}
public static void main(String[] args) {
try {
Map<String, Object> data = new HashMap<String, Object>();
data.put("name", "段然涛");
data.put("sex", "男");
data.put("birthday", "1994-03-14");
data.put("phone", "17737138812");
data.put("address", "河南省许昌市");
data.put("school", "江西科技师范大学");
List<Map<String, String>> educations = new ArrayList<Map<String, String>>();
Map<String, String> paramsMap = new HashMap<String, String>();
paramsMap.put("school", "禹州一高");
paramsMap.put("startDate", "2008-09");
paramsMap.put("endDate", "2012-06");
paramsMap.put("person", "李磊");
Map<String, String> paramsMap2 = new HashMap<String, String>();
paramsMap2.put("school", "江西科技师范大学");
paramsMap2.put("startDate", "2012-09");
paramsMap2.put("endDate", "2016-07");
paramsMap2.put("person", "李杰");
educations.add(paramsMap);
educations.add(paramsMap2);
data.put("educations", educations);
//List<String> images = new ArrayList<String>();
//images.add(getImageBase("C:/Users/Administrator/Desktop/图片/timg.jpg"));
//images.add(getImageBase("C:/Users/Administrator/Desktop/图片/timg11.jpg"));
//data.put("images", images);
} catch (Exception e) {
e.printStackTrace();
}
}
}
步骤:
先画好需要导出的模板,循环导出的数据(最好画一个表格,这样循环的时候自动会换行)。
把1.word换成1.xml结尾的。最好格式化一下xml看成清楚。然后把xml中需要放数据的地方,换成${data}(数据)。
最后把1.xml换成1.ftl结尾的。OK