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)渲染结果

相关推荐
CodeAmaz6 分钟前
RocketMQ整体工作流程_详解
java·rocketmq·rocketmq整体流程
刘一说6 分钟前
ES6+核心特性全面浅析
java·前端·es6
大学生资源网7 分钟前
基于springboot的农村综合风貌展示平台设计与实现(源码+文档)
java·数据库·spring boot·后端·毕业设计·源码·springboot
czlczl2002092512 分钟前
Spring Boot Filter 机制与 FilterRegistrationBean
java·spring boot·后端
严文文-Chris14 分钟前
RAG关键技术要点详解
java·服务器·前端
❀͜͡傀儡师17 分钟前
基于docker一键部署 x86的cpu_mem_hog 用于生成CPU和内存负载,用于服务器cpu和内存使用不达标的
java·服务器·docker
蜡笔大新79819 分钟前
IO流的认识(2)
java·ide·intellij-idea
廋到被风吹走29 分钟前
【Java】【JVM】OOM 原因、定位与解决方案
java·开发语言·jvm
苹果醋334 分钟前
vue + iview + vue-i18n中英翻译
java·运维·spring boot·mysql·nginx
橙露39 分钟前
VMware Workstation Pro 25H2的linux版本,免费分享,下载:全新命名体系 + 深度适配 Linux 内核,虚拟化效率拉满
java·linux·服务器