如何利用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("文件下载异常");
    }
}
相关推荐
胡闹542 小时前
海康和大华厂商的RTSP取流地址格式进行拉流直播
java·网络
手揽回忆怎么睡2 小时前
Java集成whisper.cpp
java·开发语言·whisper
无名-CODING2 小时前
栈与队列学习笔记
java·笔记
Hui Baby2 小时前
LSM 原理、实现及与 B+ 树的核心区别
java·linux·算法
NZT-482 小时前
C++基础笔记(二)队列deque,queue和堆priority_queue
java·c++·笔记
Tadas-Gao2 小时前
存储技术革命:SSD、PCIe与NVMe的创新架构设计与性能优化
java·性能优化·架构·系统架构·存储
codergjw2 小时前
常见面试题
java
咕噜企业分发小米2 小时前
如何平衡服务器内存使用率和系统稳定性?
java·服务器·前端
李子园的李2 小时前
函数式编程与传统编程的对比——基于java
java
爬山算法2 小时前
Netty(13)Netty中的事件和回调机制
java·前端·算法