官网有完整的例子:http://deepoove.com/poi-tl/#hack-loop-table
1.导包
(注意:低版本没有LoopRowTableRenderPolicy,下面是目前最新版)
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.12.0</version>
</dependency>
2.需要动态添加的模板
3.使用
部分代码
String fileName = type + ".docx";
String outPath = "D:\\iedaworks\\test-service\\src\\main\\resources\\file\\wordTest.docx";
InputStream inputStream = WordConvertPdf.downloadFileForLocal(fileName);
LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
Configure configure = Configure.builder().bind("laji", policy).build();
XWPFTemplate template = XWPFTemplate.compile(inputStream, configure).render(
Collections.unmodifiableMap(new HashMap<String, Object>() {
{
List<NbsjBean> forms = JSON.parseArray(bzData.toString(), NbsjBean.class);
for (int i = 0; i < forms.size(); i++) {
int xh = i+1;
forms.get(i).setXh(xh+"");
put("xh" + i, xh+"");
put("bzdxxm" + i, forms.get(i).getBzdxxm());
put("bzdxsfzh" + i, forms.get(i).getBzdxsfzh());
put("xsbzxzfqk" + i, forms.get(i).getXsbzxzfqk());
put("xsqkcxjg" + i, forms.get(i).getXsqkcxjg());
put("jlscsj" + i, forms.get(i).getJlscsj());
put("clzt" + i, forms.get(i).getClzt());
}
put("laji", forms);
}
})
);
File file = new File(outPath);
if (file.exists()){
file.delete();
}
template.writeToFile(outPath);
template.close();
}
|