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

打印 上一主题 下一主题

主题 889|帖子 889|积分 2671

简介:

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

poi-tl官网文档 Poi-tl Documentation

1. word格式

这是我的test.word


这是导出后的out.docx文件





2. 依靠

首先pom.xml导入依靠
  1.                 <dependency>
  2.                         <groupId>org.apache.poi</groupId>
  3.                         <artifactId>poi</artifactId>
  4.                         <version>5.2.2</version>
  5.                 </dependency>
  6.                 <dependency>
  7.                         <groupId>org.apache.poi</groupId>
  8.                         <artifactId>poi-ooxml</artifactId>
  9.                         <version>5.2.2</version>
  10.                 </dependency>
  11.                 <dependency>
  12.                         <groupId>com.deepoove</groupId>
  13.                         <artifactId>poi-tl</artifactId>
  14.                         <version>1.12.0</version>
  15.                 </dependency>
复制代码

3. 实现代码

  1. import com.deepoove.poi.XWPFTemplate;
  2. import com.deepoove.poi.config.Configure;
  3. import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
  4. import java.io.*;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. public class EditWordUtil {
  9.     public static void main(String[] args) {
  10.         String filePath = "D:/test.docx";
  11.         try (FileInputStream resourceAsStream = new FileInputStream(new File(filePath));) {
  12.             // 组装测试数据
  13.             Map<String, Object> map = new HashMap<>();
  14.             map.put("title", "日记");
  15.             map.put("text", "好日子");
  16.             map.put("score", 55);
  17.             map.put("reward", "妈妈奖励我10块钱");
  18.             map.put("imgUrl", "https://img2.baidu.com/it/u=485011689,3022056151&fm=253&fmt=auto&app=120&f=PNG?w=176&h=176");
  19.             ArrayList<Map<String, Object>> list = new ArrayList<>();
  20.             for (int i = 0; i < 4; i++) {
  21.                 HashMap<String, Object> maps = new HashMap<>();
  22.                 maps.put("name", "name" + i);
  23.                 maps.put("age", "age" + i);
  24.                 maps.put("address", "address" + i);
  25.                 list.add(maps);
  26.             }
  27.             map.put("lists", list);
  28.             // 嵌套map数据
  29.             HashMap<Object, Object> table = new HashMap<>();
  30.             table.put("listTitle", "这是表格标题!");
  31.             map.put("table", table);
  32.             // 调用生成 Word 文件方法,将结果保存到本地
  33.             EditWordUtil.createWordOfList("D:/out.docx", resourceAsStream, map);
  34.         } catch (IOException e) {
  35.             e.printStackTrace();
  36.         }
  37.     }
  38.     // 将生成的 Word 保存到本地文件系统
  39.     public static boolean createWordOfList(String outputPath, InputStream templatePath, Map<String, Object> dates) throws IOException {
  40.         try (FileOutputStream out = new FileOutputStream(outputPath);
  41.              BufferedOutputStream bos = new BufferedOutputStream(out)) {
  42.             // 使用Configure.ConfigureBuilder而不是Builder
  43.             ConfigureBuilder builder = Configure.builder();
  44.             LoopRowTableRenderPolicy loopRowTableRenderPolicy = new LoopRowTableRenderPolicy();
  45.             // 动态绑定
  46.             for (Map.Entry<String, Object> entry : dates.entrySet()) {
  47.                 if (entry.getValue() instanceof ArrayList) {
  48.                     builder.bind(entry.getKey(), loopRowTableRenderPolicy);
  49.                 }
  50.             }
  51.             Configure configure = builder.build();
  52.             // 读取模板并渲染数据
  53.             XWPFTemplate template = XWPFTemplate.compile(templatePath, configure).render(dates);
  54.             try {
  55.                 template.write(bos);
  56.                 template.close();
  57.             } catch (Exception e) {
  58.                 e.printStackTrace();
  59.                 return false;
  60.             }
  61.             out.flush();
  62.             bos.flush();
  63.         }
  64.         return true;
  65.     }
复制代码


小结:

平凡对象字段使用 {{ }} 两个花括号包裹字段。
循环使用的是{{lists}},循环的内容是用中括号 [ ] 包裹的字段。
注意:{{ }}和[ ] 包裹字段的时候不能有空格,否则word渲染不上。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

刘俊凯

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表