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

使用poi-tl循环导出word报表

先看模板和导出的效果
模板
在这里插入图片描述
效果
在这里插入图片描述
根据模板循环生成表格,每个表格再循环填充数据,也就是两层循环,第一层循环是学生学期信息,第二层循环是学生的成绩数据。

第一个循环

{{?listTable}} 
{{/}}

第二个循环

{{reportList}}
表格中[]是具体的属性,比如[displayName]表示课程名称

核心代码

maven

<dependency><groupId>com.deepoove</groupId><artifactId>poi-tl</artifactId><version>1.10.5</version>
</dependency>
@Test
public void export() throws IOException {// 模板文件ClassPathResource classPathResource = new ClassPathResource("/doc/template.docx");InputStream inputStream = classPathResource.getInputStream();Map<String, Object> map = new HashMap<>();String str = FileUtil.readFileByPath("C:\\Users\\chenggaowei\\Desktop\\score.json");List<StudentCourseVO> studentList = new ArrayList<>();// 模拟写入多个学生的数据for (int i = 0; i < 3; i++) {StudentCourseVO vo = new StudentCourseVO();vo.setStudentName("张三" + (i + 1));vo.setPeriodName("2023-2024学年上学期");List<ReportField> reportList = JSONObject.parseArray(str, ReportField.class);vo.setReportList(reportList);studentList.add(vo);}// 循环的数据,listTable和模板对应map.put("listTable", studentList);LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();// reportList和模板对应Configure config = Configure.builder().bind("reportList", policy).build();XWPFTemplate template = XWPFTemplate.compile(inputStream, config).render(map);File outputFile = FileUtil.createTempFile("out.docx");template.writeToFile(outputFile.getAbsolutePath());FileUtil.openFolder(outputFile);}

学业课程数据vo


/*** 学业课程数据* @author chenggaowei* @date 2023/10/8*/
@Data
public class StudentCourseVO {/*** 学生id*/private String studentId;/*** 学生姓名*/private String studentName;/*** 学期id*/private String periodId;/*** 学期名称*/private String periodName;/*** 成绩数据*/private List<ReportField> reportList;
}
http://www.lryc.cn/news/187306.html

相关文章:

  • PyCharm搭建Scrapy环境
  • TensorFlow的transformer类模型文件转换为pytorch
  • C++学习之指针和数组
  • 什么是站内搜索引擎?如何在网站中加入站内搜索功能?
  • 【C++】面向对象编程(六)在派生类中定义一个虚函数、虚函数的静态解析
  • uniapp vue3 静态图片引入
  • 仅用61行代码,你也能从零训练大模型
  • Vue3目录结构与Yarn.lock 的版本锁定
  • 内网渗透之哈希传递
  • Haar cascade+opencv检测算法
  • 跨域请求方案整理实践
  • Git Pull failure 【add/commit】
  • 单链表习题(对应章节chapter2)
  • SQL创建新表
  • Python视频剪辑-Moviepy视频尺寸和颜色调整技巧
  • 前端笔记:Create React App 初始化项目的几个关键文件解读
  • 提高工作效率!本地部署Stackedit Markdown编辑器,并实现远程访问
  • visual studio解决bug封装dll库
  • 合肥工业大学自然语言处理实验报告
  • layui laydate实现日期选择并禁用指定的时间
  • scala数组函数合集
  • 软件测试「转行」答疑(未完更新中)
  • 计算机网络---TCP/UDP
  • Docker私有仓库打开2375端口(linux)
  • 底部Taber的抽取
  • Bootstrap中固定某一个元素不随滚动条滚动
  • 时序数据库 IoTDB 发布端边云原生解决方案,有效优化工业互联网数据上传时效与资源消耗...
  • Spring Boot中实现发送文本、带附件和HTML邮件
  • MySQL5.7版本与8.0版本在CentOS系统安装
  • 【gitlab】从其他仓库创建项目