如何利用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("文件下载异常");
    }
}
相关推荐
巴拉巴拉~~几秒前
KMP 算法通用步进器组件:KmpStepperWidget 横向 / 纵向 + 匹配进度 + 全样式自定义
java·服务器·开发语言
贺今宵2 分钟前
使用idea启动一个springboot项目
java·ide·intellij-idea
伍一5113 分钟前
芋道框架下的进销存升级(三):Yudao-ERP2异步导出/导入Excel的设计与实现
java·excel·异步导出excel
胡闹5413 分钟前
【EasyExcel】字段赋值错乱问题
java·开发语言
断剑zou天涯14 分钟前
【算法笔记】AC自动机
java·笔记·算法
张工摆Bug14 分钟前
《别再写满屏的if-else了!Spring Boot + 策略模式实战优化》
java
独自归家的兔16 分钟前
基于GUI-PLUS 搭配 Java Robot 实现智能桌面操控
java·开发语言·人工智能
用户37215742613517 分钟前
Python 实现 PDF 文档压缩:完整指南
java
ew4521817 分钟前
【JAVA】实现word的DOCX/DOC文档内容替换、套打、支持表格内容替换。
java·开发语言·word
贺今宵18 分钟前
装Maven并在idea上配置
java·maven·intellij-idea