工具类org.freemarker freemarker 2.3.30
package com.cbnb.eob.base.utils;
import freemarker.template.Configuration;
import freemarker.template.Template;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.Map;
public class WordUtils {
private static Configuration configuration = null;
//这里注意的是模板要放在resources/templete文件夹下
private static final String templateFolder = WordUtils.class.getClassLoader().getResource("").getPath()+"templete/";
static {
configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");
try {
configuration.setDirectoryForTemplateLoading(new File(templateFolder));
} catch (IOException e) {
e.printStackTrace();
}
}
private WordUtils() {
throw new AssertionError();
}
public static void exportMillCertificateWord(HttpServletRequest request, HttpServletResponse response,String name, Map map) throws IOException {
//模板名称
Template freemarkerTemplate = configuration.getTemplate(name+".ftl");
File file = null;
InputStream fin = null;
ServletOutputStream out = null;
try {
// 调用工具类的createDoc方法生成Word文档
file = createDoc(map,freemarkerTemplate);
fin = new FileInputStream(file);
response.setCharacterEncoding("utf-8");
response.setContentType("application/msword");
// 设置浏览器以下载的方式处理该文件名
String fileName = name+".doc";
response.setHeader("Content-Disposition", "attachment;filename="
.concat(String.valueOf(URLEncoder.encode(fileName, "UTF-8"))));
out = response.getOutputStream();
byte[] buffer = new byte[1024]; // 缓冲区
int bytesToRead = -1;
// 通过循环将读入的Word文件的内容输出到浏览器中
while((bytesToRead = fin.read(buffer)) != -1) {
out.write(buffer, 0, bytesToRead);
}
out.flush();
} finally {
if(fin != null) {
fin.close();
}
if(out != null) {
out.close();
}
if(file != null) {
file.delete(); // 删除临时文件
}
}
}
private static File createDoc(Map dataMap, Template template) {
String name = "test.doc";
File f = new File(name);
Template t = template;
try {
// 这个地方不能使用FileWriter因为需要指定编码类型否则生成的Word文档会因为有无法识别的编码而无法打开
Writer w = new OutputStreamWriter(new FileOutputStream(f), "utf-8");
t.process(dataMap, w);
w.close();
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
return f;
}
}
工具类使用
//查询符合性审查信息
List voList = queryComplianceInspectionByItemId(projectId);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//获取不用循环的数据
Map dataMap = new HashMap<>();
dataMap.put("projectNo", voList.get(0).getProjectNo());
dataMap.put("projectName", voList.get(0).getProjectName());
dataMap.put("kbDate",formatter.format(voList.get(0).getKbDate()));
dataMap.put("address", voList.get(0).getAddress());
//获取需要循环的数据
List
导出模板制作
1、先word中将要填充的属性填写在对应的位置上
2、将word另存为xml格式的文件
3、使用Sublime Text 或者其他软件打开xml文件
4、将文件保存为UTF-8的格式
5、在文件中查找对应的字段,使用
包
裹
住
,
如
{}包裹住,如
包裹住,如{projectNo},全部替换完保存
6、将文件后缀名改为flt格式
7、将ftl文件复制到项目下resources/templete
1、将其格式化 ctrl+alt+l
2、找到需要循环的行,w:tr 为行,w:tc 为列,使用list标签包裹住
<#list mpList as mpList>
${mpList?index+1}
<#if mpList.deptName??>${mpList.deptName}#if>
<#if mpList.fitExamination??>${mpList.fitExamination}#if>
<#if mpList.details??>${mpList.details}#if>
#list>
3、字段使用 mpList.xxx填充
<#if mpList.deptName??>${mpList.deptName}#if>
4、序号从1开始
${mpList?index+1}



