简介:
开发中我们需要通过在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而不是Builder
- ConfigureBuilder 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渲染不上。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |