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();
    }
}

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

相关推荐
Java面试题总结37 分钟前
Spring Boot:别再重复造轮子,这些内置功能香麻了
java·spring boot·后端
海兰1 小时前
【第32篇】场景示例项目
人工智能·spring boot·状态模式·spring ai
夕除2 小时前
spring boot
java·spring boot·后端
敖正炀3 小时前
boot-boost 项目架构设计文档
spring boot·spring
空中海4 小时前
03 MyBatis Spring Boot 集成、事务、测试与工程化体系
spring boot·后端·mybatis
杨运交4 小时前
[013][缓存模块]基于Redis的计数器缓存模板设计——AbstractCounterCacheTemplate 技术解析
spring boot·后端
RuoyiOffice5 小时前
SpringBoot+Vue3 企业假期余额系统设计:账户、流水、预占、销假退回与到期清零全链路拆解
spring boot·后端·spring·vue·hr·企业管理软件·ruoyioffice
FYKJ_201016 小时前
springboot校园兼职平台--附源码02041
java·javascript·spring boot·python·eclipse·django·php
hERS EOUS17 小时前
SpringBoot 使用 spring.profiles.active 来区分不同环境配置
spring boot·后端·spring
kybs199121 小时前
springboot视频推荐系统--附源码72953
java·spring boot·python·eclipse·asp.net·php·idea