java操作word循环动态添加表格Table使用poi-tl的LoopRowTableRenderPolicy

通过这个简单的例子,再去和自己的业务结合

官网http://deepoove.com/poi-tl/

官网有其它完整的例子:http://deepoove.com/poi-tl/#hack-loop-table

1.导包

( 注意:poi-tl低版本没有LoopRowTableRenderPolicy ,下面是目前最新版)

<dependency>
  <groupId>com.deepoove</groupId>
  <artifactId>poi-tl</artifactId>
  <version>1.12.0</version>
</dependency>

<!-- JSON类需要这个依赖 -->
 <dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>fastjson</artifactId>
     <version>1.2.73</version>
 </dependency>

2.需要动态添加的模板

在这里插入图片描述

3.使用

3.1 需要循环添加内容的实体类

package org.app.test;

public class TestLoopRowTable {
    private String xh;
    private String nr;

    public TestLoopRowTable(String xh, String nr) {
        this.xh = xh;
        this.nr = nr;
    }

    public TestLoopRowTable() {
    }

    @Override
    public String toString() {
        return "TestLoopRowTable{" +
                "xh='" + xh + '\'' +
                ", nr='" + nr + '\'' +
                '}';
    }

    public String getXh() {
        return xh;
    }

    public void setXh(String xh) {
        this.xh = xh;
    }

    public String getNr() {
        return nr;
    }

    public void setNr(String nr) {
        this.nr = nr;
    }
}

3.2 获取数据和模板进行填充

import com.alibaba.fastjson.JSON;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

public class TestTable {

    @org.junit.jupiter.api.Test
    public  void testTable() throws IOException {
//        compile 编译模板
//        render 渲染数据
//        write 输出到流
//        TDO模式:Template + data-model = output
        // 模板文件地址
        String filePath= "D:\\codes\\08\\miaosha\\src\\main\\resources\\static\\tableTest.docx";
        // 读取模板后保存生成word的地址
        String outPath = "D:\\codes\\08\\miaosha\\src\\main\\resources\\static\\tableTestOut.docx";
        // 为表格的显示绑定行循环
        LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
        // 将bz设置为行循环绑定的数据源的key,即key是bz的value会在模板中的{{bz}}处进行解析
        Configure configure = Configure.builder().bind("bz", policy).build();
        // 将需要解析的数据放到map中
        HashMap<String, Object> map = new HashMap<String, Object>() {
            {
                // 其他业务获取到数据源
                String testTable = "[{\"xh\":\"1\",\"nr\":\"内容1\"},{\"xh\":\"2\",\"nr\":\"内容2\"},{\"xh\":\"3\",\"nr\":\"内容3\"}]";
                // 内容在表格里循环
                // JSON使用,需要导入fastjson依赖
                List<TestLoopRowTable> forms = JSON.parseArray(testTable, TestLoopRowTable.class);
                for (int i = 0; i < forms.size(); i++) {
                    put("xh" + i, forms.get(i).getXh());
                    put("nr" + i, forms.get(i).getNr());
                }
                put("bz", forms);
            }
        };
        // 读取模板、数据并渲染
        XWPFTemplate template = XWPFTemplate.compile(filePath, configure).render(map);
//         文件是否已存在,则删除
                File file = new File(outPath);
                if (file.exists()){
                    file.delete();
                }
//         生成word保存在指定目录
                template.writeToFile(outPath);
                template.close();
    }
}

输出结果:
在这里插入图片描述

  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值