当前位置: 首页 > news >正文

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

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

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

 

http://www.lryc.cn/news/280956.html

相关文章:

  • chatgpt和文心一言哪个更好用
  • 移动端开发进阶之蓝牙通讯(一)
  • 一个完整的流程表单流转
  • 2024杭州国际智慧城市,人工智能,安防展览会(杭州智博会)
  • 编程笔记 html5cssjs 031 HTML视频
  • SpringBoot外部配置文件
  • 99个Python脚本实用实例
  • HarmonyOS 工程目录介绍
  • 门店管理系统驱动智慧零售升级
  • Iterator迭代器操作集合元素时,不能用集合删除元素
  • Spring Boot是什么-特点介绍
  • 相机成像之图像传感器与ISP【四】
  • 新手入门Java 方法带参,方法重载及面向对象和面向过程的区别介绍
  • 使用Sqoop将Hive数据导出到TiDB
  • 互联网上门洗衣洗鞋工厂系统搭建;
  • Redis面试题12
  • el-tree多个树进行节点同步联动(完整版)
  • python两个字典合并,两个list合并
  • 搜维尔科技:【简报】元宇宙数字人赛道,《全息影像技术应用》!
  • SparkSQL和Hive语法差异
  • XCODE IOS 静态链接库替换升级
  • API设计:从基础到优秀实践
  • 路由的安装顺序
  • 华为OD机试真题-围棋的气--Java-OD统一考试(C卷)
  • CANFD数据记录仪在新能源汽车复杂路测下的应用
  • java: 5-6 break
  • 如何使用Imagewheel搭建一个简单的的私人图床无公网ip也能访问
  • 响应式编程Reactor API大全(上)
  • vue3自定义指令
  • ECharts 多季度连续显示到一个图中。