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

一、引入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. 生成文件

参考:

使用最全

讲解最全

  • 2
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
poi-tl是一个用于生成Word文档的Java模板引擎,它可以方便地创建和操作Word表格。在使用poi-tl生成Word表格时,你需要添加以下依赖到你的项目中: ```xml <dependency> <groupId>com.deepoove</groupId> <artifactId>poi-tl</artifactId> <version>1.10.0</version> </dependency> ``` 通过使用poi-tl提供的API,你可以轻松地创建和设置表格的样式。例如,如果你想要整行加粗,可以使用`textBold()`方法来设置行数据的样式。以下是一个示例代码: ```java RowRenderData titleRow = Rows.of(firstLine.getTitle(), null, null, null, null, null).textBold().create(); ``` 如果你想要指定特定单元格的样式,比如让表格中每一行的第二列内容加粗并且为红色字体,你可以使用以下方法: ```java CellStyle boldAndRedTextStyle = new CellStyle(); boldAndRedTextStyle.setDefaultParagraphStyle(ParagraphStyle.builder().withDefaultTextStyle(Style.builder().buildColor("FF00000").buildBold().build()).build()); for (RowRenderData rowRenderData : tableRenderData.getRows()) { rowRenderData.getCells().get(1).setCellStyle(boldAndRedTextStyle); } ``` 这样,你就可以使用poi-tl生成带有特定样式的Word表格了。 #### 引用[.reference_title] - *1* [java通过poi-tl模板引擎生成表格(Word)](https://blog.csdn.net/qq_45731464/article/details/119247125)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [[编程] POI-TL 根据模版生成Word文档的一些使用技巧汇总](https://blog.csdn.net/lmcboy/article/details/128216186)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值