Freemaker通过制定好的word模板,将数据绑定到变量中,然后转换成word,实现导出下载功能。
1、制作word模板
另存为xml格式的文件,然后将里面诸如allOrgs等变量,替换成${allOrgs}这种(如果表格中的内容需要对list变量进行循环,则加入<#list serviceBefFiveList as listKey>这种标签)。
2、后台代码
制作完模板,开始代码,首先maven依赖:
org.freemarker
freemarker
2.3.20
FreeMakerUtil实现模板读取,变量绑定,并将数据写入到bean中
package com.neusoft.freemaker.utils;
import java.io.StringWriter;
import java.util.Map;
import com.neusoft.freemaker.entity.ExportFileBean;
import freemarker.template.Configuration;
import freemarker.template.Template;
public class FreeMarkerUtil {
public static ExportFileBean createWordStream(Map<String, Object> dataMap,String templateName,String fileName){
ExportFileBean bean =new ExportFileBean();
try {
//创建配置实例
Configuration configuration = new Configuration();
//设置编码
configuration.setDefaultEncoding("UTF-8");
//word模板位置
configuration.setClassForTemplateLoading(FreeMarkerUtil.class,"/com/neusoft/freemaker/template/");
//获取模板
Template template = configuration.getTemplate(templateName);
StringWriter swriter = new StringWriter();
//生成 文件字符串
template.process(dataMap, swriter);
bean.setFileName(fileName);
bean.setIs(swriter.toString().getBytes("UTF-8"));;
} catch (Exception e) {
e.printStackTrace();
}
return bean;
}
}
ExportFileBean
package com.neusoft.freemaker.entity;
public class ExportFileBean {
private String fileName;
private byte[] is;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public byte[] getIs() {
return is;
}
public void setIs(byte[] is) {
this.is = is;
}
}
功能下载类
package com.neusoft.freemaker.controller;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.neusoft.core.web.BaseController;
import com.neusoft.freemaker.entity.ExportFileBean;
import com.neusoft.freemaker.utils.FreeMarkerUtil;
@Controller
@RequestMapping(value = "/freemaker")
public class FreemakerController extends BaseController {
@RequestMapping(value = "/downFile")
//@ResponseBody 这个不要,json返回格式才需要这个
public void downFile(HttpServletResponse response) throws Exception {
Map<String, Object> dataMap = new HashMap<String, Object>();
//获取测试map数据
this.getReportMapData(dataMap);
String templateName = "testModel.xml";
String fileName = dataMap.get("titleName")+"质控报告("+ dataMap.get("reportDate")+").doc";
ExportFileBean efb = FreeMarkerUtil.createWordStream(dataMap, templateName, fileName);
//1.设置文件ContentType类型,这样设置,会自动判断下载文件类型
response.setContentType("multipart/form-data");
//2.设置文件头:最后一个参数是设置下载文件名(假如我们叫zms.jpg,这里是设置名称)
response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));
ServletOutputStream out=null;
try {
//3.通过response获取ServletOutputStream对象(out)
out = response.getOutputStream();
out.write(efb.getIs());
out.flush();
out.close();
} finally{
try{
if(out!=null){
out.close();
out.flush();
}
}catch (IOException e) {
e.printStackTrace();
}
}
public void getReportMapData(Map<String, Object> dataMap) throws Exception {
dataMap.put("titleName", "XXX");
dataMap.put("reportDate", "2019-12-01~2019-12-10");
dataMap.put("districtName", "xxx市");
dataMap.put("disLevel", "市属");
dataMap.put("subLevel", "区县");
dataMap.put("allOrgs", "58");
dataMap.put("completeNums", "24");
dataMap.put("incomNums", "34");
dataMap.put("comRate", "41.38%");
dataMap.put("cityOrgs", "26");
dataMap.put("cityComOrgs", "14");
dataMap.put("cityInComOrgs", "12");
dataMap.put("cityComRate", "53.85%");
dataMap.put("counOrgs", "32");
dataMap.put("counComOrgs", "10");
dataMap.put("counInComOrgs", "22");
dataMap.put("counComRate", "31.25%");
dataMap.put("phisOrgs", "288");
dataMap.put("phisComOrgs", "0");
dataMap.put("phisInComOrgs", "288");
dataMap.put("phisComRate", "0%");
}
}
3、前台js代码:
function dodownload(){
window.location.href=basePath+"/freemaker/downFile";
}
点击按钮,进入dodownload()函数,然后就会像其他网站下载内容一下,在浏览器下侧出现下载的内容
4、注意事项:
String fileName = dataMap.get("titleName")+"质控报告("+ dataMap.get("reportDate")+").doc";
这里设置文件后缀时,一定不要设置成docx,否则下载下来的word打开会报错