一.准备工作
导入坐标
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.10.4</version>
</dependency>
二.Word插入附件
1.模板
直接用attachement占位
2.代码实现
文件备注已经写明具体执行情况
public void create() throws IOException {
//1.创建文件对象
AttachmentRenderData attach = Attachments
.ofLocal("src/main/resources/report_template.xlsx", AttachmentType.XLSX) // 附件文档,Word或者Excel
.create();
//2.添加数据
Map<String, Object> data = new HashMap<>();
data.put("attachment", attach);
//3.绑定附件插件
Configure config = Configure.builder()
.bind("attachment", new AttachmentRenderPolicy()) // 绑定标签和附件插件
.build();
//4.读取模板,填充数据,输出数据
XWPFTemplate.compile("src/main/resources/word.docx", config)//读取模板
.render(data)
.writeAndClose(new FileOutputStream("src/main/resources/结果.docx"));//输出结果
}