当前位置: 首页 > news >正文

SpringBoot poi-tl通过模板占位符生成word文件

简介:

        开发中我们需要通过在word中使用占位符来动态渲染一些数据,本文讲解poi-tl实现动态生成word文档,包括表格循环,对象嵌套。


poi-tl官网文档 Poi-tl Documentation

1. word格式

这是我的test.word

这是导出后的out.docx文件

2. 依赖

首先pom.xml导入依赖

		<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>5.2.2</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>5.2.2</version></dependency><dependency><groupId>com.deepoove</groupId><artifactId>poi-tl</artifactId><version>1.12.0</version></dependency>

3. 实现代码

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;public class EditWordUtil {public static void main(String[] args) {String filePath = "D:/test.docx";try (FileInputStream resourceAsStream = new FileInputStream(new File(filePath));) {// 组装测试数据Map<String, Object> map = new HashMap<>();map.put("title", "日记");map.put("text", "好日子");map.put("score", 55);map.put("reward", "妈妈奖励我10块钱");map.put("imgUrl", "https://img2.baidu.com/it/u=485011689,3022056151&fm=253&fmt=auto&app=120&f=PNG?w=176&h=176");ArrayList<Map<String, Object>> list = new ArrayList<>();for (int i = 0; i < 4; i++) {HashMap<String, Object> maps = new HashMap<>();maps.put("name", "name" + i);maps.put("age", "age" + i);maps.put("address", "address" + i);list.add(maps);}map.put("lists", list);// 嵌套map数据HashMap<Object, Object> table = new HashMap<>();table.put("listTitle", "这是表格标题!");map.put("table", table);// 调用生成 Word 文件方法,将结果保存到本地EditWordUtil.createWordOfList("D:/out.docx", resourceAsStream, map);} catch (IOException e) {e.printStackTrace();}}// 将生成的 Word 保存到本地文件系统public static boolean createWordOfList(String outputPath, InputStream templatePath, Map<String, Object> dates) throws IOException {try (FileOutputStream out = new FileOutputStream(outputPath);BufferedOutputStream bos = new BufferedOutputStream(out)) {// 使用Configure.ConfigureBuilder而不是BuilderConfigureBuilder builder = Configure.builder();LoopRowTableRenderPolicy loopRowTableRenderPolicy = new LoopRowTableRenderPolicy();// 动态绑定for (Map.Entry<String, Object> entry : dates.entrySet()) {if (entry.getValue() instanceof ArrayList) {builder.bind(entry.getKey(), loopRowTableRenderPolicy);}}Configure configure = builder.build();// 读取模板并渲染数据XWPFTemplate template = XWPFTemplate.compile(templatePath, configure).render(dates);try {template.write(bos);template.close();} catch (Exception e) {e.printStackTrace();return false;}out.flush();bos.flush();}return true;}


小结:

普通对象字段使用 {{ }} 两个花括号包裹字段。

循环使用的是{{lists}},循环的内容是用中括号 [ ] 包裹的字段。

注意:{{ }}和[ ] 包裹字段的时候不能有空格,否则word渲染不上。


http://www.mrgr.cn/news/57633.html

相关文章:

  • 054_python基于爬虫与文本挖掘的网络舆情监控系统
  • 使用原生HTML和css制作一个箭头步骤条
  • 数字图像处理---第一章 图像处理基础
  • HTML、CSS 和 JavaScript 的介绍
  • iPhone图片/照片/视频复制到win10系统的简单方法 - 照片导出
  • 7. 配置
  • 第一批用AI的设计师,已经彻底赚翻了!
  • 网站怎么做的?
  • 【语音转文本新体验】Windows部署Whisper Web结合内网穿透轻松远程转录——“cpolar内网穿透”
  • 【vue2.7.16系列】手把手教你搭建后台系统__selectIcon和svgIcon组件(12)
  • 1024节的由来?程序员节的由来?
  • Springboot网络安全培训平台-计算机毕业设计源码88959
  • R语言笔记(二):向量
  • 基础数据结构——队列(双端队列,优先级队列,阻塞队列)
  • 【MySQL】C语言连接MySQL数据库3——事务操作和错误处理API
  • C++中指针类型、引用类型、值类型
  • 面试必备:RabbitMQ与Kafka核心知识点总结
  • 使用 SpaCy 和 NLTK 进行文本处理与切片详解
  • 中酱集团:黑松露酱油,天然配方定义健康生活
  • 【golang】学习文档整理
  • 11 怎么给字符串字段加索引?
  • js 基础补充3
  • 【面试题】如果 Redis 遇到 Hash 冲突了该怎么处理?
  • 代码随想录-哈希表-快乐数
  • 安装报错解决:No module named ‘quaternion‘
  • 无线路由器设置成交换机(补充)