使用spring boot freemarker导出word模板
- 遇到坑一: 导出的word里面像是 .xml文件信息,空置太保存,显示有某个字段出错
- 遇到坑二:加载不到模板
最近在项目中遇到一个功能,从数据库中获取数据,然后将这些数据写入word模板中,网上也有很多资料和样例,主要步骤大概就是创建一个word文件,word文件中需要数据库保存的地方用占位符来写:${}
文件创建好之后先另存为其他格式,选择(.xml)格式
将另存为的,xml格式文件复制到项目目录下的resource/template目录下,这时候将文件的格式修改为ftl格式。
到这里开始工作差不多已经完成,下面主要编写业务代码:
@RequestMapping("/
## enterprise
/getDoc/{id}")
public void getDoc(@PathVariable Long id,
HttpServletRequest request, HttpServletResponse response) throws Exception {
RegisterEnterpriseDTO registerEnterpriseDTO = registerEnterpriseService.getDetail(id);
Map<String,String> dataMap = new HashMap<String,String>();
dataMap.put("name", "拟申请名称")
dataMap.put("enterpriseProperty", "企业性质");
dataMap.put("industry","行业类别";
dataMap.put("regAddress", "注册地址");
dataMap.put("address", "经营地址");
dataMap.put("regCapital","注册资本");
dataMap.put("exportShareholder", "股东信息");
dataMap.put("legalName", "法人代表");
dataMap.put("legalCard", "法人代表证件号");
dataMap.put("legalTel", "联系电话");
dataMap.put("contact", "联系人名称");
dataMap.put("contactIdentityCard", "联系人证件号");
dataMap.put("contactTel", "联系电话");
dataMap.put("businessScope", "经营范围";
//设置表明
String newWordName = "企业入驻信息采集表.doc";
//调用打印word的函数
DocUtil.download(request, response, newWordName, dataMap);
}
导出帮助类
import freemarker.cache.FileTemplateLoader;
import freemarker.cache.TemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.Map;
public class DocUtil {
public static void download(HttpServletRequest request, HttpServletResponse response, String newWordName, Map dataMap) throws Exception{
Configuration configuration = new Configuration(Configuration.getVersion());
configuration.setDefaultEncoding("utf-8");
Template t = null;
//加载模板所在的目录,这里面遇到很坑的问题
configuration.setClassForTemplateLoading(configuration.getClass(), "/template");
try {
//word.xml是要生成Word文件的模板文件
t = configuration.getTemplate("word.ftl","utf-8");
} catch (Exception e) {
e.printStackTrace();
}
File outFile = null;
Writer out = null;
String filename = newWordName;
try {
outFile = new File(newWordName);
out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(outFile),"utf-8"));
} catch (Exception e1) {
e1.printStackTrace();
}
try {
t.process(dataMap, out);
} catch (Exception e) {
e.printStackTrace();
}
try {
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
InputStream fis = null;
OutputStream toClient = null;
try {
fis = new BufferedInputStream(new FileInputStream(outFile));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
filename = URLEncoder.encode(filename, "utf-8");
response.addHeader("Content-Disposition", "attachment;filename=" + filename+"");
response.addHeader("Content-Length", "" + outFile.length());
toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
if(fis!=null){
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(toClient!=null){
toClient.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
到这里导出功能基本上就完成,但是在时间开发中遇到了各种坑:
遇到坑一: 导出的word里面像是 .xml文件信息,空置太保存,显示有某个字段出错
经检测最后确定 在word的模板中所有的占位符都需要有对用的值,不能是null,如果在业务中获取的某个字段是null,需要手动去赋予空字符串,就是不能是null,占位符要和map里面的数据对应上,map里面的数据不能比模板里面的占位符少
遇到坑二:加载不到模板
Freemarker提供了3种加载模板目录的方法。 它使用Configuration类加载模板
3种方法分别是:
一、基于类路径:
public void setClassForTemplateLoading(Class clazz, String pathPrefix);
Configuration cfg = new Configuration();
cfg.setClassForTemplateLoading(cfg.class, "/template");
cfg.getTemplate("word.ftl");
这里注意一下第二个参数需要以 “/” 开头。
二、文件系统:
public void setDirectoryForTemplateLoading(File dir) throws IOException;
Configuration cfg = new Configuration();
cfg.setDirectoryForTemplateLoading(new File("/home/user/template"));
cfg.getTemplate("word.ftl");
三、Servlet Context:
public void setServletContextForTemplateLoading(Object servletContext, String path);
看名字也就知道了,分别基于类路径、文件系统以及Servlet Context。
我开始使用的是第二种,但是在使用idea启用的时候完全没有问题,但是在项目部署的时候获取不到路径,我是用spring boor 生成的jar,无法获取,使用绝对路径更是不行,在new File的时候出错,提示模板路径不存在。后来使用第一种方式,用类加载的方式,然后使用的是相对路径。部署之后成功,在idea中也没有问题。
导出帮助类也是在网上找的,里面部分代码在我在不适用,稍作修改