如何利用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("文件下载异常");
    }
}

相关推荐

  1. POI导入导出

    2024-03-18 07:42:01       20 阅读
  2. poi导出值班excel

    2024-03-18 07:42:01       24 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-18 07:42:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-18 07:42:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-18 07:42:01       82 阅读
  4. Python语言-面向对象

    2024-03-18 07:42:01       91 阅读

热门阅读

  1. 他山之石可以攻玉

    2024-03-18 07:42:01       42 阅读
  2. 渗透测试基础技能树梳理

    2024-03-18 07:42:01       40 阅读
  3. Keras库搭建神经网络

    2024-03-18 07:42:01       44 阅读
  4. 重拾C++之菜鸟刷算法第14篇---贪心算法

    2024-03-18 07:42:01       42 阅读
  5. C语言自学笔记18----结构体指针

    2024-03-18 07:42:01       38 阅读
  6. Linux - Centos 使用screen命令

    2024-03-18 07:42:01       43 阅读
  7. Axios:贯穿前后端的数据链

    2024-03-18 07:42:01       40 阅读
  8. ArrayList和LinkedList的区别,以及应用场景

    2024-03-18 07:42:01       39 阅读
  9. flask 继续学习

    2024-03-18 07:42:01       45 阅读
  10. Spring底层核心原理解析

    2024-03-18 07:42:01       38 阅读
  11. 清理ubuntu空间

    2024-03-18 07:42:01       40 阅读
  12. ubuntu(jammy)安装docker步骤

    2024-03-18 07:42:01       40 阅读