如果是固定的值 用 {{}} 即可 但是如果是循环表格,那么就需要制定模板为如图
然后在处理表格数据时候:
/**
* 传入 节点对象 返回生成的word文档
* @param flangeJoint
* @return
* @throws IOException
*/
private XWPFTemplate getXwpfTemplate(CmComplaintEntity flangeJoint) throws IOException {
Map<String, Object> map = new HashMap<>();
// 准备表格循环数据
List<Map<String, Object>> tableData = prepareTableData(flangeJoint);// map.put("table", table);
map.put("jointNo", "123");
map.put("table", tableData);
// 导出
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource resource = resolver.getResource("classpath:/templates/jointReport_en.docx");
Configure config = Configure.builder().bind("table", new LoopRowTableRenderPolicy()).build();
XWPFTemplate template = XWPFTemplate.compile(resource.getInputStream(), config).render(map);
// XWPFTemplate template = XWPFTemplate.compile(resource.getInputStream()).render(map);
return template;
}
// 准备表格循环数据
private List<Map<String, Object>> prepareTableData(CmComplaintEntity entity) {
List<Map<String, Object>> list = new ArrayList<>();
for (int i = 0; i < 6; i++) {
Map<String, Object> row = new HashMap<>();
row.put("index", i + 1); // 序号
row.put("name", "名称"+ i); // 名称
row.put("value", "值"+i); // 值
list.add(row);
}
return list;
}
具体 全部的导出逻辑 请看我的上一期Java如何导出word(根据模板生成),通过word转成pdf,放压缩包-CSDN博客