【POI-导出word-One】poi-tl根据word模板去渲染数据(不包含图片)
poi-tl官网:https://deepoove.com/poi-tl/
引包版本(maven版),如果版本不一致,很有可能导致有些底层方法调用失败:
<!-- 代码生成器结束 --> <!-- poi-tl --> <dependency> <groupId>com.deepoove</groupId> <artifactId>poi-tl</artifactId> <version>1.12.0</version> </dependency> <!-- poi-ooxml --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.2.2</version> </dependency> <!-- poi --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>5.2.2</version> </dependency> <!-- poi-ooxml-schemas --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>4.1.2</version> </dependency> <!-- poi-scratchpad --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-scratchpad</artifactId> <version>5.2.2</version> <scope>compile</scope> </dependency>
以下只是放在本地测试的简易代码:word模板,里面的字段占位符设置格式:{{xm}}
使用场景:渲染多个对象,并且每个对象渲染一次word模板,第二个对象渲染模板的是另起一页,最后放在一个word里面;我觉得这样操作使用的好处就是,导出的模板格式不需要自己通过代码手动去调,直接新建一个word,或者用下发的word格式
放入占位符,就可以直接按照原来的模板生成,字体,字体大小,边框这些都不需要通过后端代码手动去调整,这个是最方便的,如果有轻微的模板格式调整,只要占位符不变,直接替换模板就行,不需要重新改代码,再重新打包部署。
@PostMapping("/testWord2") @ApiOperation(value = "测试接口", notes = "测试接口") public void testWord2() { String templatePath = "C:\\Users\\WordTest.docx"; String outputPath = "C:\\Users\\ExportedDoc"+ DateUtils.dateTimeNow() +".docx"; try (XWPFDocument document = new XWPFDocument(new FileInputStream(templatePath)); FileOutputStream out = new FileOutputStream(outputPath)) { // 删除模板中的第一页表格或内容 if (!document.getTables().isEmpty()) { document.removeBodyElement(document.getPosOfTable(document.getTables().get(0))); } List<Map<String, Object>> records = new ArrayList<>(); Map<String,Object> map = new HashMap<>(); map.put("xm","张哈哈哈"); map.put("xb","-------"); map.put("lxdh","========="); records.add(map); Map<String,Object> map2 = new HashMap<>(); map2.put("xm","张嘻嘻嘻"); map2.put("xb","-------"); map2.put("lxdh","========="); records.add(map2); for (Map<String, Object> record : records) { XWPFTemplate template = XWPFTemplate.compile(templatePath); template.render(record); for (XWPFTable table : template.getXWPFDocument().getTables()) { document.createTable(); document.setTable(document.getTables().size() - 1, table); } if (record != records.get(records.size() - 1)) { document.createParagraph().setPageBreak(true); } template.close(); } document.write(out); } catch (IOException e) { e.printStackTrace(); } }
这个只是一个最简易的版本,在本地运行导出,实际情况,一般是Linux服务器部署,所以获取的模板的方式和导出的方式肯定是要有更改的,再一个,要考虑导出的模板在我们服务器是不是要保存一份呢?
Hello!希望可以共同进步!一起讨论吧~
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理