poi-tl
列表
这个列表的导出可以是多段文字,例如需要分成几段进行展示,每段的前面添加一定的有序的序号或者无序的序号。
官方给的模板语法是:{{*var}}。
后端 java 对应的数据格式为 :\
List<String>
NumberingRenderData
其中 List 中的泛型不一定是 String 类型,也可以是其它的对象,如果写的对象然后我们按照官方给的语法,那么输出的结果就和我们在 java 中使用 toString() 的方法类似,会把里面所有的信息给打印出来。
NumberingRenderData 是poi-tl 给的类型,一般只是显示的情况下就是用 List 就可以满足绝大部分的场景。\具体的实现,模板部分:\
java 代码部分:
List<String> data = new ArrayList()
data.add("这是第一行");
data.add("这是第二行");
XWPFTemplate template = XWPFTemplate.compile("D:\template.docx").render(
new HashMap<String, Object>(){{
put("data", data)
}});
try {
template.writeAndClose(new FileOutputStream("D:\out.docx"));
}catch (Exception e){
e.printStackTrace();
}
最终展示的结果:
区块对
区块对的作用就是当满足区块对的条件时,才会显示区块对中的内容,如果不满足则不显示内容。
官方提供的写法:{{?sections}}{{/sections}}
注意: 区块对没办法直接使用之前在网上找资料的时候看到有区块对写法没问题但是不起作用的情况,这里需要在 java 中开启才行。 开启的写法:
ConfigureBuilder configureBuilder = Configure.builder();
configureBuilder.useSpringEL(false);
默认是 true,在文档中进行使用区块对,模板部分:
java 代码部分:
XWPFTemplate template = XWPFTemplate.compile("D:\template.docx").render(
new HashMap<String, Object>(){{
put("data", "1")
}});
try {
template.writeAndClose(new FileOutputStream("D:\out.docx"));
}catch (Exception e){
e.printStackTrace();
}
模板导出后的结果:
区块对合理使用几乎可以适用任何场景。