最近要生成word文档,就学习了一下。
添加maven依赖
<!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
1、在word模板中定义好占位符${}形式,另存为.xml格式,然后重命名为.ftl格式
2、将test.ftl放入相应文件夹下
(1)工具类
import freemarker.template.Configuration;
import freemarker.template.Template;
import sun.misc.BASE64Encoder;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
public class ExportDocFreeMarker {
private Configuration configuration;
private String encoding;
public ExportDocFreeMarker(String encoding) {
this.encoding = encoding;
configuration = new Configuration(Configuration.VERSION_2_3_22);
configuration.setDefaultEncoding(encoding);
//configuration.setClassForTemplateLoading(this.getClass(), "/com/cars/bjcyqs/util/print");
try {
//模板所在文件路径,Freemarker提供了3种加载模板目录的方法,此处用的是基于文件系统
configuration.setDirectoryForTemplateLoading(new File("E:/bjcyqs/bjcyqs/bjcyqs-web/target/bjcyqs-web/file"));
} catch (IOException e) {
e.printStackTrace();
}
}
public Template getTemplate(String name) throws Exception {
return configuration.getTemplate(name);
}
public String getImageStr(String image) throws IOException {
InputStream is = new FileInputStream(image);
BASE64Encoder encoder = new BASE64Encoder();
byte[] data = new byte[is.available()];
is.read(data); is.close();
return encoder.encode(data);
}
public Map<String, Object> getDataMap() {
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put("bj0","中华人民共和国首都");
dataMap.put("bj1","政治中心");
dataMap.put("bj2","天津");
dataMap.put("bj3","河北");
dataMap.put("bj4","永定河、潮白河、北运河、拒马河");
dataMap.put("bj5","2154.2");
dataMap.put("bj6","30320");
dataMap.put("bj7","14");
/*try {
dataMap.put("image", getImageStr("D:\\头像.jpg"));
} catch (IOException e) {
e.printStackTrace();
}*/
return dataMap;
}
public void exportDoc(HttpServletResponse response,String name, String doc) throws Exception {
//将模板和数据合并成文件
Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(doc), encoding));
try {
//生成文件
getTemplate(name).process(getDataMap(), writer);
writer.flush();
InputStream input = null;
OutputStream outputString = null;
try {
response.setHeader("Content-Disposition","attachment; filename=" + URLEncoder.encode( "测试文件.doc", "UTF-8"));//直接下载doc文档
input = new BufferedInputStream(new FileInputStream(new File(doc)));
outputString = new BufferedOutputStream(response.getOutputStream());
EchartPdfUtil.copy(input, outputString);
outputString.flush();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
try {
input.close();
outputString.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (null != writer) {
writer.close();
}
}
}
public static void main(String[] args) throws Exception {
ExportDocFreeMarker maker = new ExportDocFreeMarker("UTF-8");
//maker.exportDoc("D:\\test.doc", "test.ftl");
}
(2)控制类
@RequestMapping("/exportDoc")
public void exportPdf(HttpServletRequest request, HttpServletResponse response) throws Exception {
ExportDocFreeMarker maker = new ExportDocFreeMarker("UTF-8");
try {
maker.exportDoc(response,"testFreeMarker.ftl","D:/test.doc");//在D盘生成文件
} catch (Exception e) {
e.printStackTrace();
}
}
(3)生成的文档