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

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

相关推荐
V+zmm10134几秒前
在线办公小程序(springboot论文源码调试讲解)
vue.js·spring boot·微信小程序·小程序·毕业设计
m0_7482359527 分钟前
SpringBoot:解决前后端请求跨域问题(详细教程)
java·spring boot·后端
坚定信念,勇往无前2 小时前
springboot单机支持1w并发,需要做哪些优化
java·spring boot·后端
剑走偏锋o.O3 小时前
Java四大框架深度剖析:MyBatis、Spring、SpringMVC与SpringBoot
java·spring boot·spring·mybatis
风月歌3 小时前
基于springboot校园健康系统的设计与实现(源码+文档)
java·spring boot·后端·mysql·毕业设计·mybatis·源码
m0_748239473 小时前
Spring Boot框架知识总结(超详细)
java·spring boot·后端
m0_748236113 小时前
Spring Boot 实战:轻松实现文件上传与下载功能
linux·spring boot·后端
m0_748245924 小时前
Spring Boot(七):Swagger 接口文档
java·spring boot·后端
尚学教辅学习资料4 小时前
基于SpringBoot的宠物服务系统+uniapp小程序+LW参考示例
spring boot·uni-app·宠物
青灯文案15 小时前
如何在 SpringBoot 项目使用 Redis 的 Pipeline 功能
spring boot·redis·后端