小工具知识网

当前位置: 首页 > 行业知识 > poi-tl的使用(动态表格的生成)

行业知识

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

2026-12-07 04:36:00 来源:互联网转载

一、引入pom

            com.deepoove            poi-tl            1.12.0

注意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工厂方法来创建图片数据
             org.apache.poi            poi            5.2.2                            org.apache.poi            poi-ooxml            5.2.2                            org.apache.poi            poi-ooxml-schemas            5.2.2        

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

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

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

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

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

  1. 代码
@Datapublic class People {    private String age;    private String index;    private  String name;    private String note;    private ArrayList axisNoteList;}@Datapublic 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 datas = new HashMap();        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 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 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. 生成文件

参考:

使用最全

讲解最全

上一篇:速运通怎么设置服务器?

下一篇:影廊视频下载秘籍,轻松掌握高效下载技巧!