第一步:准备要导出的word文件,将需要填充的内容使用${****}替换,之后将word文件的相关样式设置好,例如页码等等。如图:
第二步:将word另存为.xml文件 ,使用记事本打开xml文件,之后使用在线xml格式化工具格式化,在myeclipse中创建一个ftl文件,将格式化的xml内容复制进ftl文件中。将其中“${”和“deptName”和“}”中间的内容删除如图:
替换之后的内容如图:
其他的类似。替换完成之后freemarker模板就已经准备好了,放入项目中,后台导出生成word文件导出时需要使用。
第三步:后台导出word。
//用于读取ftl文件
Configuration config = new Configuration();
config.setDefaultEncoding("UTF-8");
config.setDirectoryForTemplateLoading(new File(template));
config.setObjectWrapper(new DefaultObjectWrapper());
//以utf-8格式读取ftl文件
Template t = config.getTemplate("test.ftl", "UTF-8");
//创建临时文件
File f = new File("aa.doc");
Writer out = new OutputStreamWriter(new FileOutputStream(f), "utf-8");
Map<String,Object> aa = new HashMap<>();
aa.put("deptName", "部门");
t.process(aa, out);
out.flush();
out.close();
response.reset();
String filename = "dd.doc";
response.setCharacterEncoding("utf-8");
response.setContentType("application/msword");
response.setHeader("Content-Disposition","attachment; filename=".concat(new String(filename.getBytes("gb2312"), "iso8859-1")));
OutputStream os = response.getOutputStream();
InputStream fin = new FileInputStream(f);
byte[] buffer = new byte[512]; // 缓冲区
int bytesToRead = -1;
// 通过循环将读入的Word文件的内容输出到浏览器中
while((bytesToRead = fin.read(buffer)) != -1) {
os.write(buffer, 0, bytesToRead);
}
os.flush();
os.close();
if(f!=null){
f.delete();
}
以上内容为导出一份word,例如导出的是一份成绩单。如果想把全年级的成绩单导出的话,我们需要稍微调整一下以上的内容。
第一步:同上类似。只是需要增加做一个循环功能。将需要填充的内容用“${student.userName!!}”等替换。
第二步:同上操作。需要再在这两个位置循环填充内容。
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml">
<w:body>
<!--需要遍历数据 循环开始-->
*<#assign pageNo =0><#list datas as student><#assign pageNo = pageNo+1>*
<!--**********省略*********-->
</w:sectPr>
<!--循环结束-->
</#list>
</w:body>
</w:document>
第三步:后台导出word文件实现。
//用于读取ftl文件
Configuration config = new Configuration();
config.setDefaultEncoding("UTF-8");
//template模板文件的路径
config.setDirectoryForTemplateLoading(new File(template));
config.setObjectWrapper(new DefaultObjectWrapper());
//以utf-8格式读取ftl模板文件
Template t = config.getTemplate("test.ftl", "UTF-8");
//从数据库查询数据
List<Map> datas = service.queryAllData();
//创建临时文件
File f = new File("aa.doc");
Writer out = new OutputStreamWriter(new FileOutputStream(f), "utf-8");
Map<String,Object> aa = new HashMap<>();
aa.put("datas", datas);
t.process(aa, out);
out.flush();
out.close();
response.reset();
String filename = "test.doc";
response.setCharacterEncoding("utf-8");
response.setContentType("application/msword");
response.setHeader("Content-Disposition","attachment; filename=".concat(new String(filename.getBytes("gb2312"), "iso8859-1")));
OutputStream os = response.getOutputStream();
InputStream fin = new FileInputStream(f);
byte[] buffer = new byte[512]; // 缓冲区
int bytesToRead = -1;
// 通过循环将读入的Word文件的内容输出到浏览器中
while((bytesToRead = fin.read(buffer)) != -1) {
os.write(buffer, 0, bytesToRead);
}
os.flush();
os.close();
if(f!=null){
f.delete();
}