依赖
<!-- word导出 --><dependency><groupId>com.deepoove</groupId><artifactId>poi-tl</artifactId><version>1.7.3</version></dependency><!-- 上面需要的依赖--><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.1.2</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>4.1.2</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.1.2</version></dependency>
代码
public static void main(String[] args) throws IOException {String filePath = "D:\\test\\巡查日志.docx";XWPFTemplate template = XWPFTemplate.compile(filePath);// 填充数据Map<String, Object> data = new HashMap<>();data.put("inspectionTime", "John");data.put("deptName", 30);template.render(data);File file = new File("D:\\test\\test1.docx");// 保存Word文档FileOutputStream out = new FileOutputStream(file);template.write(out);out.close();}
模板使用{{占位符}}