我是靠谱客的博主 含糊月光,这篇文章主要介绍java 根据ftl文件生成wordjava 根据ftl文件生成word,现在分享给大家,希望可以做个参考。

java 根据ftl文件生成word

复制代码
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
public void exportDoc(String path,String name ,Map data){ try { //Configuration 用于读取ftl文件 Configuration configuration = new Configuration(new Version("2.3.0")); configuration.setDefaultEncoding("utf-8"); /** * 以下是两种指定ftl文件所在目录路径的方式,注意这两种方式都是 * 指定ftl文件所在目录的路径,而不是ftl文件的路径 */ //指定路径的第一种方式(根据某个类的相对路径指定) // configuration.setClassForTemplateLoading(this.getClass(), ""); //指定路径的第二种方式,我的路径是C:/a.ftl configuration.setDirectoryForTemplateLoading(new File(path)); //输出文档路径及名称 File outFile = new File(path + "/"+ name+ ".doc"); //以utf-8的编码读取ftl文件 Template template = configuration.getTemplate(name + ".ftl", "utf-8"); Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"), 10240); template.process(data, out); out.close(); } catch (Exception e) { e.printStackTrace(); } }

map的格式为key-value形式,ftl文件需要修改的值为${key}

ftl文档:

循环:

复制代码
1
2
3
4
<#list lists as list> ${list.name} </#list>

判断:
判断需要判空,否则报错

复制代码
1
2
3
4
<#if tj?? && tj = '1'> <#else> </#if>

表格居中显示:
在表格的表头tr中添加:

复制代码
1
2
3
4
5
6
7
<w:tr> <w:trPr> <w:trHeight w:val="250"/> <w:jc w:val="center"/> </w:trPr> </w:tr>

最后

以上就是含糊月光最近收集整理的关于java 根据ftl文件生成wordjava 根据ftl文件生成word的全部内容,更多相关java内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(117)

评论列表共有 0 条评论

立即
投稿
返回
顶部