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

Apache POI 实现 Excel 表格下载

这里以苍穹外卖中数据导出功能为例,记录下 Apache POI 导出 Excel 表格的过程。
导出数据报表
首先在 pom.xml 中导入相关依赖

<!-- poi 用于操作 excel 表格-->
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId></dependency>

controller层

注意方法中的参数 response, service层定义输出流对象要用。

@RestController
@RequestMapping("/admin/report")
@Slf4j
public class ReportController {@GetMapping("/export")@ApiOperation("/导出运营报表")public void exportExcel(HttpServletResponse response){// 注意这里的参数 response, service层定义输出流对象要用。reportService.exportExcel(response);}
}

Service层

定义输出流对象时要用 response 去定义,不是直接 new FileOutputStream()
在得到模板文件时候,注意新建的文件夹在resource下,不要写错,刚开始定义的文件夹名字叫 template, 没加字母 ‘s’,一直获取不到模板文件,正确的应该叫 templates

@Service
public class ReportServiceImpl implements ReportService {@Overridepublic void exportExcel(HttpServletResponse response) {// 得到最近三十天的起止日期LocalDate beginTime = LocalDate.now().minusDays(30);LocalDate endTime = LocalDate.now().minusDays(1);BusinessDataVO businessDataVo = workspaceService.getBusinessData(LocalDateTime.of(beginTime, LocalTime.MIN), LocalDateTime.of(endTime, LocalTime.MAX));// 得到模板文件InputStream in = this.getClass().getClassLoader().getResourceAsStream("templates/运营数据报表模板.xlsx");try {XSSFWorkbook excel = new XSSFWorkbook(in);// 写入时间XSSFSheet sheet = excel.getSheetAt(0);sheet.getRow(1).getCell(1).setCellValue("时间:" + beginTime + "~" + endTime);// 写入概览数据sheet.getRow(3).getCell(2).setCellValue(businessDataVo.getTurnover());sheet.getRow(3).getCell(4).setCellValue(businessDataVo.getOrderCompletionRate());sheet.getRow(3).getCell(6).setCellValue(businessDataVo.getNewUsers());sheet.getRow(4).getCell(2).setCellValue(businessDataVo.getValidOrderCount());sheet.getRow(4).getCell(4).setCellValue(businessDataVo.getUnitPrice());// 填充明细数据for (int i = 0; i < 30; i++) {LocalDate date = beginTime.plusDays(i);BusinessDataVO businessData = workspaceService.getBusinessData(LocalDateTime.of(date, LocalTime.MIN), LocalDateTime.of(date, LocalTime.MAX));XSSFRow row = sheet.getRow(7 + i);row.getCell(1).setCellValue(date.toString());row.getCell(2).setCellValue(businessData.getTurnover());row.getCell(3).setCellValue(businessData.getValidOrderCount());row.getCell(4).setCellValue(businessData.getOrderCompletionRate());row.getCell(5).setCellValue(businessData.getUnitPrice());row.getCell(6).setCellValue(businessData.getNewUsers());}// 输出流下载文件ServletOutputStream out = response.getOutputStream();excel.write(out);// 关闭资源out.flush();out.close();excel.close();} catch (IOException e) {e.printStackTrace();} finally {try {in.close();} catch (IOException e) {e.printStackTrace();}}}}

实现效果

在这里插入图片描述

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

相关文章:

  • 大华嵌入式面试题大全及参考答案(2万字长文)
  • C语言——查漏补缺
  • Python | Leetcode Python题解之第328题奇偶链表
  • 吉瑞外卖笔记
  • Perl套接字编程指南:构建网络通信应用
  • 达梦数据库(十) -------- mybatis-plus 整合达梦时,自动生成的 sql 语句报错
  • 停止项目大小调整,开始搜索层自动缩放!
  • VScode的环境编译器选择
  • 在Linux中通过docker安装和配置supervisor进程守护
  • CanMV-K230自学笔记系列(不定期更新)
  • [GXYCTF2019]禁止套娃-使用无参数读文件
  • SpringBoot+MyBatis模板
  • Springboot 定时任务 @EnableScheduling @Scheduled
  • STM32F407ZET6使用LCD(9341)
  • 动手学深度学习7.3 网络中的网络(NiN)-笔记练习(PyTorch)
  • SQL语言-select的使用方法
  • 深入理解Python中的排序算法:快速排序与归并排序实现
  • Linux基础命令 ② 未完成
  • 怎么加密文件?分享文件加密四个方法,2024新版操作教程,教你搞定!
  • cesium加载魔方立方体
  • unity 粒子系统学习
  • CogVideoX环境搭建推理测试
  • STL—容器—list【list的介绍和基本使用】【list的迭代器失效问题】
  • 【面试宝典】MySQL 面试问题
  • 【Cpp筑基】三、对象和类
  • 数据库原理面试-核心概念-问题理解
  • 【JavaScript】JavaScript里的“先斩后奏”之王 shift()方法
  • Python面试宝典第32题:课程表
  • 简单介绍BTC的Layer2项目RGB
  • 跨境电商卖家必看:搭建安全稳定测评自养号环境系统