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

如何利用POI导出报表

一、报表格式

二、依赖坐标

<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.16</version>
</dependency>
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.16</version>
</dependency>

三、 controller层代码

/*
* 导出运营表
* */@GetMapping("/export")@ApiOperation("导出运营数据报表")public void export(HttpServletResponse response) throws Exception {reportService.exportBusinessData(response);}

四、导出报表

@Override
public void exportBusinessData(HttpServletResponse response) {//查询30天的数据LocalDateTime endTime = LocalDateTime.now();LocalDateTime startTime = endTime.minusDays(30).toLocalDate().atStartOfDay();BusinessDataVO businessDataVO = workspaceService.businessData(startTime, endTime);log.info("businessDataVO,{}", businessDataVO.toString());//查询每一天的List<BusinessDataVO> list = new ArrayList<>();while (startTime.isBefore(endTime)) {LocalDateTime end = startTime.plusHours(24).minusSeconds(1);BusinessDataVO dayVO = workspaceService.businessData(startTime, end);list.add(dayVO);log.info("startTime{},dayVO{}", startTime, dayVO);startTime = startTime.plusDays(1);}try {//读取excel模板InputStream in = this.getClass().getClassLoader().getResourceAsStream("templates/运营数据报表模板.xlsx");XSSFWorkbook workbook = new XSSFWorkbook(in);//填充模板XSSFSheet sheet = workbook.getSheetAt(0);//填充总的30天的XSSFRow row3 = sheet.getRow(3);XSSFRow row4 = sheet.getRow(4);row3.getCell(2).setCellValue(businessDataVO.getTurnover() + "");row3.getCell(4).setCellValue(businessDataVO.getOrderCompletionRate() + "");row3.getCell(6).setCellValue(businessDataVO.getNewUsers() + "");row4.getCell(2).setCellValue(businessDataVO.getValidOrderCount() + "");row4.getCell(4).setCellValue(businessDataVO.getUnitPrice() + "");//填充每一天的int i = 0;startTime = endTime.minusDays(30).toLocalDate().atStartOfDay();for (BusinessDataVO dayVO : list) {XSSFRow row = sheet.getRow(7 + i);if (row == null) {row = sheet.createRow(7 + i);}row.getCell(1).setCellValue(startTime.toLocalDate().toString());row.getCell(2).setCellValue(dayVO.getTurnover() + "");row.getCell(3).setCellValue(dayVO.getValidOrderCount() + "");row.getCell(4).setCellValue(dayVO.getOrderCompletionRate() + "");row.getCell(5).setCellValue(dayVO.getUnitPrice() + "");row.getCell(6).setCellValue(dayVO.getNewUsers() + "");i++;startTime = startTime.plusDays(1);}//写入到response的输出流ServletOutputStream outputStream = response.getOutputStream();workbook.write(outputStream);//关流outputStream.close();workbook.close();} catch (Exception e) {log.error(e.getMessage(), e);throw new BaseException("文件下载异常");}
}

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

相关文章:

  • 自动部署SSL证书到阿里云腾讯云CDN
  • 【系统性】 循序渐进学C++
  • rust - 一个日志缓存记录的通用实现
  • elasticsearch(RestHighLevelClient API操作)(黑马)
  • 用尾插的思想实现移除链表中的元素
  • 【Kubernetes】k8s删除master节点后重新加入集群
  • HCIP—OSPF虚链路实验
  • RAxML-NG安装与使用-raxml-ng-v1.2.0(bioinfomatics tools-013)
  • Tomcat内存马
  • pytorch之诗词生成3--utils
  • OpenAI的ChatGPT企业版专注于安全性、可扩展性和定制化。
  • JS06-class对象
  • 深度学习1650ti在win10安装pytorch复盘
  • Node.js与webpack(三)
  • 测试覆盖率那些事
  • Etcd 介绍与使用(入门篇)
  • Docker 安装 LogStash
  • Selenium笔记
  • ChatGPT :确定性AI源自于确定性数据
  • linux驱动开发面试题
  • 【AI】Ubuntu系统深度学习框架的神经网络图绘制
  • AI推介-大语言模型LLMs论文速览(arXiv方向):2024.03.05-2024.03.10—(2)
  • AI解答——DNS、DHCP、SNMP、TFTP、IKE、RIP协议
  • 【TypeScript系列】声明合并
  • zookeeper基础学习之六: zookeeper java客户端curator
  • MySQL数据库操作学习(2)表查询
  • Java学习
  • C#八皇后算法:回溯法 vs 列优先法 vs 行优先法 vs 对角线优先法
  • springboot整合swagger,postman,接口规范
  • 029—pandas 遍历行非向量化修改数据