Java用freemarker导出word
概述
详细
最近一个项目要导出word文档,折腾老半天,发现还是用freemarker的模板来搞比较方便省事,现总结一下关键步骤,供大家参考,这里是一个简单的试卷生成例子。
一、模板的制作
先用Word做一个模板,如下图:
(注意,上面是有表格的,我设置了边框不可见)然后另存为XML文件,之后用工具打开这个xml文件,有人用firstobject XML Editor感觉还不如notepad++,我这里用notepad++,主要是有高亮显示,和元素自动配对,效果如下:
上面黑色的地方基本是我们之后要替换的地方,比如xytitle替换为${xytitle},对已表格要十分注意,比如选择题下面的表格,我们可以通过<w:tr>查找来定位,一对<w:tr></w:tr>代表一行,也就是一条记录(一道题),我们这里要用一对<#list></#list>来将其包括,以便后续填充数据,具体可参照Freemarker页面语法,例如这里选择题,我们是两行为一条记录,所以要<#list></#list>要包括两行,形如:<#list table1 as plan1><w:tr>题号 题目</w:tr><w:tr>选项</w:tr></#list>,然后在这其中找着对应的xzn,xztest,ans1,ans2,ans3,ans4替换为${plan1.xzn},${plan1.xztest},${plan1.ans1},${plan1.ans2},${plan1.ans3},${plan1.ans4},注意这里的table1及plan1命名,table1后续填充数据要用到,其他的替换同理操作,得到效果如下:
保存后,修改后缀名为ftl,至此模板制作完毕。
二、编程实现
这里用到了freemarker-2.3.13.jar包,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | package common; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.io.Writer; import java.util.Map; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; public class DocumentHandler { private Configuration configuration = null ; public DocumentHandler() { configuration = new Configuration(); configuration.setDefaultEncoding( "utf-8" ); } public void createDoc(Map<String,Object> dataMap,String fileName) throws UnsupportedEncodingException { //dataMap 要填入模本的数据文件 //设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载, //这里我们的模板是放在template包下面 configuration.setClassForTemplateLoading( this .getClass(), "/template" ); Template t= null ; try { //test.ftl为要装载的模板 t = configuration.getTemplate( "fctestpaper.ftl" ); } catch (IOException e) { e.printStackTrace(); } //输出文档路径及名称 File outFile = new File(fileName); Writer out = null ; FileOutputStream fos= null ; try { fos = new FileOutputStream(outFile); OutputStreamWriter oWriter = new OutputStreamWriter(fos, "UTF-8" ); //这个地方对流的编码不可或缺,使用main()单独调用时,应该可以,但是如果是web请求导出时导出后word文档就会打不开,并且包XML文件错误。主要是编码格式不正确,无法解析。 //out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile))); out = new BufferedWriter(oWriter); } catch (FileNotFoundException e1) { e1.printStackTrace(); } try { t.process(dataMap, out); out.close(); fos.close(); } catch (TemplateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //System.out.println("---------------------------"); } } |
然后是准备数据调用就行,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | package com.havenliu.document; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class Main { /** * @param args * @throws UnsupportedEncodingException */ public static void main(String[] args) throws UnsupportedEncodingException {; Map<String, Object> dataMap = new HashMap<String, Object>(); dataMap.put( "xytitle" , "试卷" ); int index = 1 ; // 选择题 List<Map<String, Object>> list1 = new ArrayList<Map<String, Object>>(); //题目 List<Map<String, Object>> list11 = new ArrayList<Map<String, Object>>(); //答案 index = 1 ; for ( int i = 0 ; i < 5 ; i++) { Map<String, Object> map = new HashMap<String, Object>(); map.put( "xzn" , index + "." ); map.put( "xztest" , "( )操作系统允许在一台主机上同时连接多台终端,多个用户可以通过各自的终端同时交互地使用计算机。" ); map.put( "ans1" , "A" + index); map.put( "ans2" , "B" + index); map.put( "ans3" , "C" + index); map.put( "ans4" , "D" + index); list1.add(map); Map<String, Object> map1 = new HashMap<String, Object>(); map1.put( "fuck" , index + "." ); map1.put( "abc" , "A" + index); list11.add(map1); index++; } dataMap.put( "table1" , list1); dataMap.put( "table11" , list11); // 填空题 List<Map<String, Object>> list2 = new ArrayList<Map<String, Object>>(); List<Map<String, Object>> list12 = new ArrayList<Map<String, Object>>(); index = 1 ; for ( int i = 0 ; i < 5 ; i++) { Map<String, Object> map = new HashMap<String, Object>(); map.put( "tkn" , index + "." ); map.put( "tktest" , "操作系统是计算机系统中的一个___系统软件_______,它管理和控制计算机系统中的___资源_________." ); list2.add(map); Map<String, Object> map1 = new HashMap<String, Object>(); map1.put( "fill" , index + "." ); map1.put( "def" , "中级调度" + index); list12.add(map1); index++; } dataMap.put( "table2" , list2); dataMap.put( "table12" , list12); // 判断题 List<Map<String, Object>> list3 = new ArrayList<Map<String, Object>>(); List<Map<String, Object>> list13 = new ArrayList<Map<String, Object>>(); index = 1 ; for ( int i = 0 ; i < 5 ; i++) { Map<String, Object> map = new HashMap<String, Object>(); map.put( "pdn" , index + "." ); map.put( "pdtest" , "复合型防火墙防火墙是内部网与外部网的隔离点,起着监视和隔绝应用层通信流的作用,同时也常结合过滤器的功能。" ); list3.add(map); Map<String, Object> map1 = new HashMap<String, Object>(); map1.put( "judge" , index + "." ); map1.put( "hij" , "对" + index); list13.add(map1); index++; } dataMap.put( "table3" , list3); dataMap.put( "table13" , list13); // 简答题 List<Map<String, Object>> list4 = new ArrayList<Map<String, Object>>(); List<Map<String, Object>> list14 = new ArrayList<Map<String, Object>>(); index = 1 ; for ( int i = 0 ; i < 5 ; i++) { Map<String, Object> map = new HashMap<String, Object>(); map.put( "jdn" , index + "." ); map.put( "jdtest" , "说明作业调度,中级调度和进程调度的区别,并分析下述问题应由哪一级调度程序负责。" ); list4.add(map); Map<String, Object> map1 = new HashMap<String, Object>(); map1.put( "answer" , index + "." ); map1.put( "xyz" , "说明作业调度,中级调度和进程调度的区别,并分析下述问题应由哪一级调度程序负责。" ); list14.add(map1); index++; } dataMap.put( "table4" , list4); dataMap.put( "table14" , list14); MDoc mdoc = new MDoc(); mdoc.createDoc(dataMap, "E:/outFile.doc" ); } } |
注意上面map中的key必须和模板中的对应,否则会报错。
三、项目导入
下载附件后,解压后,看到如下目录
将Document项目导入eclipse即可。导入后的效果如下:
会在e盘生成文件
四、运行效果
打开生成的outFile.doc则看到如下的样子:
五、freemarker语法
Freemarker页面语法:http://blog.csdn.net/thismonth/article/details/5194982
注:本文著作权归作者,由demo大师发表,拒绝转载,转载需要作者授权
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?