POI-tl 知识整理:整理3 -> 动态生成表格

1 表格行循环

(1)需要渲染的表格的模板


说明{``{goods}} 是个标准的标签,将 {``{goods}} 置于循环行的上一行,循环行设置要循环的标签和内容,注意此时的标签应该使用 [] ,以此来区别poi-tl的默认标签语法。同理,{``{labors}} 也置于循环行的上一行。

(2)定义两个实体类

java 复制代码
@Data
public class Goods {
    private int count;
    private String name;
    private String desc;
    private int discount;
    private int tax;
}



@Data
public class Labors {
    private String category;
    private int people;
    private int price;
}

(3)测试代码

java 复制代码
    @Test
    public void autoGenerateTable1() throws Exception {
        // 数据
        Goods goods1 = new Goods();
        Goods goods2 = new Goods();
        Labors labors1 = new Labors();
        Labors labors2 = new Labors();

        goods1.setCount(2);
        goods1.setName("商品1");
        goods1.setDiscount(10);
        goods1.setDesc("商品1描述");
        goods1.setTax(5);

        goods2.setCount(4);
        goods2.setName("商品2");
        goods2.setDiscount(20);
        goods2.setDesc("商品2描述");
        goods2.setTax(10);
        List<Goods> goodsList = Arrays.asList(goods1, goods2);

        labors1.setPeople(3);
        labors1.setCategory("类别1");
        labors1.setPrice(100);

        labors2.setPeople(1);
        labors2.setCategory("类别2");
        labors2.setPrice(200);
        List<Labors> laborList = Arrays.asList(labors1, labors2);

        // 用行循环插件
        LoopRowTableRenderPolicy renderPolicy = new LoopRowTableRenderPolicy();
        ConfigureBuilder builder = Configure.builder();
        Configure configure = builder.bind("goods", renderPolicy).bind("labors", renderPolicy).build();

        XWPFTemplate template = XWPFTemplate.compile("D:\\Idea-projects\\POI_word\\templates_autoGenerateTable.docx",configure);

        Map<String, Object> map = new HashMap<>();
        map.put("goods", goodsList);
        map.put("labors", laborList);

        template.render(map);

        FileOutputStream fileOutputStream = new FileOutputStream("D:\\Idea-projects\\POI_word\\output_autoGenerateTable.docx");
        template.writeAndClose(fileOutputStream);

        template.close();
    }

(4)渲染结果

(5)测试代码详细解释


2 表格列循环

列循环跟行循环没有什么区别,就是将行循环插件换成了列循环插件
(1) 模板


(2)测试代码

java 复制代码
    @Test
    public void autoGenerateTable2() throws Exception {
        // 数据
        Labors labors1 = new Labors();
        Labors labors2 = new Labors();

        labors1.setPeople(3);
        labors1.setCategory("类别1");
        labors1.setPrice(100);

        labors2.setPeople(1);
        labors2.setCategory("类别2");
        labors2.setPrice(200);
        List<Labors> laborList = Arrays.asList(labors1, labors2);

        // 用列循环插件
        LoopColumnTableRenderPolicy renderPolicy = new LoopColumnTableRenderPolicy();
        ConfigureBuilder builder = Configure.builder();
        Configure configure = builder.bind("labors", renderPolicy).build();

        XWPFTemplate template = XWPFTemplate.compile("D:\\Idea-projects\\POI_word\\templates_autoGenerateTableCol.docx",configure);

        Map<String, Object> map = new HashMap<>();
        map.put("labors", laborList);

        template.render(map);

        FileOutputStream fileOutputStream = new FileOutputStream("D:\\Idea-projects\\POI_word\\output_autoGenerateTableCol.docx");
        template.writeAndClose(fileOutputStream);

        template.close();
    }


(3)渲染结果

相关推荐
桦说编程3 小时前
Java 中如何创建不可变类型
java·后端·函数式编程
lifallen3 小时前
Java Stream sort算子实现:SortedOps
java·开发语言
IT毕设实战小研3 小时前
基于Spring Boot 4s店车辆管理系统 租车管理系统 停车位管理系统 智慧车辆管理系统
java·开发语言·spring boot·后端·spring·毕业设计·课程设计
没有bug.的程序员3 小时前
JVM 总览与运行原理:深入Java虚拟机的核心引擎
java·jvm·python·虚拟机
甄超锋4 小时前
Java ArrayList的介绍及用法
java·windows·spring boot·python·spring·spring cloud·tomcat
阿华的代码王国4 小时前
【Android】RecyclerView复用CheckBox的异常状态
android·xml·java·前端·后端
Zyy~4 小时前
《设计模式》装饰模式
java·设计模式
A尘埃4 小时前
企业级Java项目和大模型结合场景(智能客服系统:电商、金融、政务、企业)
java·金融·政务·智能客服系统
青云交5 小时前
Java 大视界 -- 基于 Java 的大数据可视化在城市交通拥堵治理与出行效率提升中的应用(398)
java·大数据·flink·大数据可视化·拥堵预测·城市交通治理·实时热力图
CHEN5_025 小时前
【Java基础面试题】Java基础概念
java·开发语言