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

1 表格行循环

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


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

 

(2)定义两个实体类 

@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)测试代码

    @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)测试代码

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

 

相关推荐

  1. PHP基础知识整理

    2024-01-18 15:40:09       24 阅读
  2. XSS基础知识整理

    2024-01-18 15:40:09       29 阅读
  3. SQLite 知识整理

    2024-01-18 15:40:09       30 阅读
  4. Flutter知识整理

    2024-01-18 15:40:09       16 阅读
  5. ORACLE 知识整理

    2024-01-18 15:40:09       18 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-18 15:40:09       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-18 15:40:09       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-18 15:40:09       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-18 15:40:09       20 阅读

热门阅读

  1. 第13章 1 进程和线程

    2024-01-18 15:40:09       28 阅读
  2. 分账系统,虚拟账户

    2024-01-18 15:40:09       40 阅读
  3. Android 自定义权限

    2024-01-18 15:40:09       33 阅读
  4. STM32-串口解析框架

    2024-01-18 15:40:09       31 阅读
  5. 消息队列之RabbitMQ介绍

    2024-01-18 15:40:09       23 阅读
  6. 蓝桥杯《蚂蚁感冒》

    2024-01-18 15:40:09       36 阅读
  7. 《设计模式的艺术》笔记 - 适配器模式

    2024-01-18 15:40:09       36 阅读
  8. docker使用指南&疑难杂症

    2024-01-18 15:40:09       42 阅读