一、需求描述
使用poi-tl技术,通过既有的word模板,使用poi-tl语法,生成定制后的word
二、maven
<!--引入poi-tl开始--> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>com.deepoove</groupId> <artifactId>poi-tl</artifactId> <version>1.4.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-scratchpad</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>ooxml-schemas</artifactId> <version>1.3</version> </dependency> <!--引入poi-tl结束-->
三、word模板
任意位置放入以下内容
模板位置
配置文件参考,此处扔到了resources下
导出路径自己随便写吧,愿意仍哪仍哪
四、代码
自己看代码吧,极其简单
public static void generateWord(List<RecordModel> recordList, String exportPath, String templatePath) throws Exception { //设置表格头 RowRenderData header = RowRenderData.build( new TextRenderData("FFF7FB", "文件名称"), new TextRenderData("FFF7FB", "文件后缀"), new TextRenderData("FFF7FB", "文件路径"), new TextRenderData("FFF7FB", "异常类别"), new TextRenderData("FFF7FB", "异常信息") ); //设置表格背景色 TableStyle tableStyle = new TableStyle(); tableStyle.setBackgroundColor("0072E3"); header.setStyle(tableStyle); //准备录入的数据 List<RowRenderData> rowList = new ArrayList<>(); for (RecordModel record : recordList) { RowRenderData row = RowRenderData.build(record.getFileName(), record.getFileSuffix(), record.getFilePath(), record.getErrorType(), record.getErrorMsg()); rowList.add(row); } //渲染表格 XWPFTemplate template = XWPFTemplate.compile(templatePath).render( new HashMap<String, Object>() {{ put("title", "GIM解析表格如下"); put("name", "我是一个name"); put("table", new MiniTableRenderData(header, rowList)); }}); //生成word template.write(new FileOutputStream(exportPath+ System.currentTimeMillis() + ".docx")); //关闭模板流 template.close(); }
五,执行代码
看个热闹就行了,就这么个意思