官网有完整的例子:http://deepoove.com/poi-tl/#hack-loop-table
1.导包(注意:低版本没有LoopRowTableRenderPolicy,下面是目前最新版)
2.需要动态添加的模板 3.使用com.deepoove poi-tl 1.12.0
部分代码
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() {
{
List 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();
}
// 生成word保存在指定目录
template.writeToFile(outPath);
template.close();
}



