Web开发经常遇到的需要,word 导出。
尝试使用过 Md2File、easypoi 国人的开源Jar 都很好用,但是不满足我的需求。
找了很多Word 的生成方式,最后选择 FreeMarker 。
如发现不对或可优化的的地方请留言我及时更正,以下记录本次功能开发的过程。
word 模板制作
先来一个简单的表格, ${reportName} = 我们要替换的变量
word 转 word 2003 xml
xml 解析
将xml 文件 格式化一下,方便查看。xml格式化 百度
将文件 poi.xml 后缀名“xml”修改为“ftl”
OK的模板数据
需要调整的,${username} 关键字被分开了,需要手动调整成上图的样子
代码实现
public void wordExport () throws IOException {
configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");
// 要填入模本的数据文件
Map dataMap = new HashMap();
dataMap.put("reportName", "张三");
// 设置模本装置方法和路径
File templePath = new File("d:/word/test/");
configuration.setDirectoryForTemplateLoading(templePath);
Template t = null;
try {
// temple.ftl为要装载的模板
t = configuration.getTemplate("poi.ftl");
t.setEncoding("utf-8");
} catch (IOException e) {
e.printStackTrace();
}
// 输出文档路径及名称
File outFile = new File("D:/word/test/"+new Date().getTime()+".doc");
Writer out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(outFile), "utf-8"));
} catch (Exception e1) {
e1.printStackTrace();
}
try {
t.process(dataMap, out);
out.close();
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
*表格数据循环
使用表达式
<#list testList as test>
表格头xml
${test.name1}
${test.name2}
${test.name3}
${test.name4}
表格尾xml
</#list>
上图中表格循环5次显示,均测试数据。
实现代码
private void getData(Map dataMap) {
dataMap.put("reportName", "张三");
//dataMap.put("resPic", getImageStr());
//dataMap.put("flawPic", getImageStr());
List<Map<String, Object>> testList= new ArrayList<>();
for(int i=1;i<=5;i++){
Map<String, Object> map=new HashMap<>();
map.put("name1", "我是小米"+i);
map.put("name2", "你好"+i);
map.put("name3", "老大"+i);
map.put("name4", "小强"+i);
testList.add(map);
}
dataMap.put("testList",testList);
}
生成的word 效果图
插入图片
基本和以上流程一样,
模板中插入照片,在xml中替换图片的base64值,代码写入新图片的base64值。
因我的word 中没图片就不提供代码了。
大家可以参考下面链接
参考文献
http://my.oschina.net/liweigov/blog/63868
http://blog.csdn.net/myfmyfmyfmyf/article/details/42641677
http://blog.csdn.net/myfmyfmyfmyf/article/details/36177979
http://blog.csdn.net/thismonth/article/details/5194982