poi-tl的使用(动态表格的生成)

文章介绍了如何在Java中使用poi-tl库结合ApachePOI5.2.2版本动态生成Word表格。通过提供模板文件,利用Data注解绑定数据,并通过LoopRowTableRenderPolicy进行行循环实例,实现内容替换和文件生成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、引入pom

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

注意apache.poi版本要对应

NOTE: poi-tl 1.12.x 要求 POI 版本在 5.2.2+.

Apache POI已经进入5.0.0+时代,如果使用低版本的Apache POI,请查阅历史版本。

当前版本 1.12.0 Documentation,Apache POI5.2.2+,JDK1.8+
1.11.x Documentation,Apache POI5.1.0+,JDK1.8+
1.10.x Documentation,Apache POI4.1.2,JDK1.8+
1.10.3 Documentation,Apache POI4.1.2,JDK1.8+
1.9.x Documentation,Apache POI4.1.2,JDK1.8+
1.8.x Documentation,Apache POI4.1.2,JDK1.8+
1.7.x Documentation,Apache POI4.0.0+,JDK1.8+
1.6.x Documentation,Apache POI4.0.0+,JDK1.8+
1.5.x Documentation,Apache POI3.16+,JDK1.6+
V1.12.0版本作了一个不兼容的改动,升级的时候需要注意:
重构了PictureRenderData,改为抽象类,建议使用Pictures工厂方法来创建图片数据
 <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>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>5.2.2</version>
        </dependency>

二、案例--动态表格的生成

创建两个文件夹,一个是用来存储模板文件,另一个是用来存储生成的文件

  1. 准备一个word模板,命名为test.docx,

注意,{{}}是官方指定的格式,也可以自定义。

[ ] 是可替换的属性 可以是多个

  1. 代码
@Data
public class People {

    private String age;
    private String index;
    private  String name;
    private String note;
    private ArrayList axisNoteList;
}
@Data
public class Student {
    private String age;
    private String index;
    private  String name;
    private String sex;
    private String sexs;
}
@Test
    public void testOne(){
        //模板地址
        String templateFilePath = "C:\\Users\\18089\\Desktop\\";
        //生成文件的保存地址
        String destFilePath = "C:\\Users\\18089\\Desktop\\";
        Map<String, Object> datas = new HashMap<String, Object>();

        Student student1 = new Student();
        student1.setAge("12");
        student1.setIndex("1");
        student1.setName("张三");
        student1.setSex("男");
        Student student2 = new Student();
        student2.setAge("13");
        student2.setIndex("2");
        student2.setName("李四");
        student2.setSex("男1");
        List<Student> studentList = Arrays.asList(student1,student2);

        People people1 = new People();
        people1.setAge("13");
        people1.setIndex("1");
        people1.setName("lisi");
        people1.setNote("测试");
        People people2 = new People();
        people2.setAge("14");
        people2.setIndex("2");
        people2.setName("lisi11");
        people2.setNote("测试11");
        List<People> peopleList = Arrays.asList(people1,people2);
        datas.put("lists2",peopleList);

        datas.put("lists",studentList);

        // 行循环实例
        LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();

        //这里可以指定一个config类,用来指定一些规则,
        Configure config = Configure.builder()
                .bind("lists2",policy)
                .bind("lists", policy).build();

        XWPFTemplate compile = XWPFTemplate.compile(templateFilePath + "test.docx",config);
        compile.render(datas);

        try {
            compile.writeToFile(destFilePath+"out_test.docx");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
  1. 生成文件

参考:

使用最全

讲解最全

评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值