poi-tl
官网地址
Poi-tl Documentationword模板引擎https://deepoove.com/poi-tl
github 地址
https://github.com/Sayi/poi-tl/tree/master
gitcode 加速地址
GitCode - 全球开发者的开源社区,开源代码托管平台GitCode是面向全球开发者的开源社区,包括原创博客,开源代码托管,代码协作,项目管理等。与开发者社区互动,提升您的研发效率和质量。https://gitcode.com/gh_mirrors/po/poi-tl/tree/master
Maven依赖
<dependency><groupId>com.deepoove</groupId><artifactId>poi-tl</artifactId><version>1.12.2</version>
</dependency>
使用场景
官方文档 有很多示例,这里就不展示里
场景一
模版设置
文字,图片,图标,循环表格 指定填充
图表设置(比较特殊)
填充结果集
代码
@GetMapping("/simple")public ResponseEntity simpleDemo() throws Exception{try(InputStream is = new ClassPathResource("/templates/template.docx").getInputStream();InputStream pictureIs = new ClassPathResource("/templates/picture/pmp.png").getInputStream();FileOutputStream os = new FileOutputStream("templates/output/template-out.docx")) {List<Cards> cards = new ArrayList<>();cards.add(new Cards().setCardNo(1).setCardName("黑魔导").setDEF(2000).setATK(2500).setCardType("暗"));cards.add(new Cards().setCardNo(2).setCardName("青眼白龙").setDEF(2500).setATK(3000).setCardType("光"));cards.add(new Cards().setCardNo(3).setCardName("火焰剑士").setDEF(1600).setATK(1800).setCardType("炎"));cards.add(new Cards().setCardNo(4).setCardName("磁石战士").setDEF(2500).setATK(3500).setCardType("暗"));LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();Configure config = Configure.builder().bind("cards", policy) // 绑定插件.build();XWPFTemplate template = XWPFTemplate.compile(is,config).render(new HashMap<String, Object>(){{put("filename", "录取通知书");put("姓名", "祖安蒙多");put("学院", "地面战斗学院");put("专业名称", "地面炮灰");put("大学名称", "火星殖民大学");put("img", Pictures.ofStream(pictureIs, PictureType.JPEG).size(100, 120).create());put("charone",Charts.ofMultiSeries("三幻神", new String[] { "攻击力", "守备力", "战斗值" }).addSeries("奥西里斯的天空龙", new Integer[] { 3000, 3500, 1200 }).addSeries("欧贝里克的巨神兵", new Integer[] { 4000, 2000, 1100 }).addSeries("拉的翼神", new Integer[] { 6000, 1000, 1400 }).create());put("cards", cards);}});template.writeAndClose(os);}return ResponseEntity.ok().build();}