freemaker可以方便实现“前后端分离”,既可以生成网页,也可以导出为word,尤其是针对那些格式样式比较复杂的文档,可以先在office中编辑好样式,然后往里面填充数据,避免了手动用代码来调节文档样式这种夸张的工作。
使用流程如下:
1.引入依赖
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.29</version>
</dependency>
2. 用office创建模板
将其中需要改变的内容改为\KaTeX parse error: Expected 'EOF', got '#' at position 102: …二个,第一个是表头)。然后用<#̲list userList a…{user.name}。注意:对于列表的处理需要在xml就开始处理,而不是像网上说的那种改为ftl文件再加标签,那样的处理方式会导致生成的word文件不能打开。
示例:
<#list userList as user>
<w:tr><w:tblPrEx><w:tblBorders><w:top w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/><w:left
w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/><w:bottom w:val="single"
w:sz="4"
wx:bdrwidth="10"
w:space="0"
w:color="auto"/><w:right
w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/><w:insideH w:val="single"
w:sz="4"
wx:bdrwidth="10"
w:space="0"
w:color="auto"/><w:insideV
w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0" w:color="auto"/></w:tblBorders><w:tblCellMar><w:left w:w="108" w:type="dxa"/>
<w:right w:w="108" w:type="dxa"/></w:tblCellMar></w:tblPrEx>
<w:trPr/>
<w:tc><w:tcPr><w:tcW w:w="3095" w:type="dxa"/><w:shd w:val="clear" w:color="auto" w:fill="auto"/><w:vAlign
w:val="top"/></w:tcPr>
<w:p><w:pPr><w:pStyle w:val="a3"/><w:rPr><w:rFonts w:ascii="仿宋_GB2312" w:h-ansi="仿宋_GB2312" w:fareast="仿宋_GB2312" w:cs="仿宋_GB2312" w:hint="fareast"/>
<w:b/>
<w:b-cs/>
<w:color w:val="auto"/>
<w:kern w:val="2"/>
<w:sz w:val="28"/>
<w:sz-cs w:val="28"/>
<w:highlight w:val="none"/>
<w:lang w:val="EN-US" w:fareast="ZH-CN"/></w:rPr></w:pPr>
<w:r><w:rPr><w:rFonts w:ascii="仿宋_GB2312" w:h-ansi="仿宋_GB2312" w:fareast="仿宋_GB2312" w:cs="仿宋_GB2312" w:hint="fareast"/><w:b/><w:b-cs/><w:color
w:val="auto"/><w:kern w:val="2"/><w:sz w:val="28"/><w:sz-cs w:val="28"/><w:highlight
w:val="none"/><w:lang w:val="EN-US" w:fareast="ZH-CN"/></w:rPr>
<w:t>${user.name}</w:t></w:r></w:p></w:tc>
<w:tc><w:tcPr><w:tcW w:w="3096" w:type="dxa"/><w:shd w:val="clear" w:color="auto" w:fill="auto"/><w:vAlign
w:val="top"/></w:tcPr>
<w:p><w:pPr><w:pStyle w:val="a3"/><w:rPr><w:rFonts w:ascii="仿宋_GB2312" w:h-ansi="仿宋_GB2312" w:fareast="仿宋_GB2312" w:cs="仿宋_GB2312" w:hint="fareast"/>
<w:b/>
<w:b-cs/>
<w:color w:val="auto"/>
<w:kern w:val="2"/>
<w:sz w:val="28"/>
<w:sz-cs w:val="28"/>
<w:highlight w:val="none"/>
<w:lang w:val="EN-US" w:fareast="ZH-CN"/></w:rPr></w:pPr>
<w:r><w:rPr><w:rFonts w:ascii="仿宋_GB2312" w:h-ansi="仿宋_GB2312" w:fareast="仿宋_GB2312" w:cs="仿宋_GB2312" w:hint="fareast"/><w:b/><w:b-cs/><w:color
w:val="auto"/><w:kern w:val="2"/><w:sz w:val="28"/><w:sz-cs w:val="28"/><w:highlight
w:val="none"/><w:lang w:val="EN-US" w:fareast="ZH-CN"/></w:rPr>
<w:t>${user.age}</w:t></w:r></w:p></w:tc>
</w:tr>
</#list>
3.将生成的xml文件扩展名改为ftl,以此作为模板。
4.在java程序中加载模板生成word文档。
public static void main(String[] args) {
try {
Map<String, Object> dataMap = new HashMap<>();
//普通的变量
dataMap.put("userName", "zq");
dataMap.put("password", "12123");
List<User> userList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
User user = new User();
user.setName("name" + i);
user.setAge(i+"");
userList.add(user);
}
//列表变量
dataMap.put("userList", userList);
freemarker.template.Configuration configuration = new Configuration();
configuration.setEncoding(Locale.getDefault(),"utf-8");
configuration.setDirectoryForTemplateLoading(new File("G:\\"));
File outPutFile = new File("G:\\result.doc");
Template template = configuration.getTemplate("template.ftl", "utf-8");
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outPutFile), "utf-8"), 10240);
template.process(dataMap, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}