poi-tl 动态生成表格 行和列都不确定

poi-tl 动态生成表格 行和列都不确定

使用poi-tl 将docx文档模板生成word文档,生成动态的表格,行数不确定,列数不确定,记录下。

1 引入pom

java 复制代码
        <!-- Poi-tl -->
        <dependency>
            <groupId>com.deepoove</groupId>
            <artifactId>poi-tl</artifactId>
            <version>1.10.5</version>
        </dependency>

2 模板 和 效果

  1. 文档模板添加

    {{#table}}

  2. 生成的文档

3 看代码

java 复制代码
package com.test;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.RowRenderData;
import com.deepoove.poi.data.Rows;
import com.deepoove.poi.data.Tables;
import org.apache.commons.collections4.list.TreeList;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DynamicColumnsWordExample {

    public static void main(String[] args) throws IOException {
        Map<String,Object> map = new HashMap<>();


        // 标题 初始化
        List<String> titleList = new TreeList<>();
        titleList.add("序号");
        titleList.add("姓名");
        for (int i = 0; i < 10; i++) {
            titleList.add(i+1+"动态列");
        }
        String [] cell = new String[titleList.size()];
        for (int i = 0; i < titleList.size(); i++) {
            cell[i]= titleList.get(i);
        }
        // 构建 标题行
        RowRenderData tableHead = Rows.of(cell).center().bgColor("3672e5").create();

        // 表格数据 初始化
        List<String> nameList = Arrays.asList("张三","李四","王武");
        List<RowRenderData> renderDataList = new TreeList<>();
        for (int i = 0; i < nameList.size(); i++) {
            List<String> list = new TreeList<>();
            list.add(i+1+"");
            list.add(nameList.get(i));
            for (int j = 0; j < 10; j++) {
                list.add(j+1+"数据");
            }
            String [] rowData = new String[list.size()];
            for (int j = 0; j < list.size(); j++) {
                rowData[j] = list.get(j);
            }
            RowRenderData row = Rows.of(rowData).center().create();
            renderDataList.add(row);
        }
        // 表格行 构建
        RowRenderData [] tableRows = new RowRenderData[renderDataList.size()+1];
        // 添加标题行
        tableRows[0] = tableHead;
        // 添加数据行
        for (int i = 0; i < renderDataList.size(); i++) {
            tableRows[i+1] = renderDataList.get(i);
        }
        map.put("table", Tables.of(tableRows).center().create());



        // 使用模板生成Word文档
        XWPFTemplate template = XWPFTemplate.compile("D:\\test_word\\test_template.docx").render(map);
        FileOutputStream out = new FileOutputStream("D:\\test_word\\test_out.docx");

        template.write(out);
        out.flush();
        out.close();
        template.close();
    }
}

如果你也学到了,麻烦点个赞,谢谢!

相关推荐
考虑考虑16 小时前
Spingboot4.2.0中的属性
spring boot·后端·spring
RuoyiOffice16 小时前
Flowable只是起点:一套OA背后的单据、资源与协作设计
java·vue.js·spring boot·flowable·企业管理·oa·ruoyi office
迦蓝叶20 小时前
自建 MaaS 卡住的地方从来不是模型:JAiRouter 开源 AI 网关的四个答案
人工智能·spring boot·开源·架构设计·maas·ai 网关
头发还在的女程序员20 小时前
家政小程序系统源码IM已读记录:多端阅读状态怎样保持单调推进
java·spring boot·源码·家政·消息同步
Diligently_21 小时前
第 08 篇|单机部署:本地安装与 Docker 最佳实践
运维·spring boot·docker
雪芽蓝域zzs2 天前
第15节:MyBatis-Plus 分页 + 条件查询(不带 Redis)
spring boot
雪芽蓝域zzs2 天前
第14章:SpringBoot3 + MyBatis‑Plus 基础 CRUD(无 Redis)
spring boot
雪芽蓝域zzs2 天前
第16节:全局统一异常优化 + 自定义业务异常
spring boot
CoderYanger3 天前
Java EE 进阶:12.Spring AOP(第二弹)
java·前端·数据库·spring boot·spring·java-ee·mybatis
CoderYanger3 天前
Java EE 进阶:12.Spring AOP(第一弹)
java·开发语言·spring boot·后端·spring·java-ee·交互