苍穹外卖项目学习日记(15)

苍穹外卖项目学习日记(15) day12

数据报表导出

  • ReportController类添加数据报表方法
  • ReportController.java
    @GetMapping("/export")
    @ApiOperation("导出数据报表")
    public void export(HttpServletResponse response) throws IOException {
        reportService.exportBusinessData(response);
    }
  • ReportService类添加数据报表接口,并且实现类中实现
  • ReportServiceImpl.java
    @Override
    public void exportBusinessData(HttpServletResponse response) throws IOException {
        //查询数据库
        LocalDate dateBegin = LocalDate.now().minusDays(30);
        LocalDate dateEnd = LocalDate.now().minusDays(1);
        BusinessDataVO businessDataVO = workspaceService.getBusinessData(LocalDateTime.of(dateBegin, LocalTime.MIN), LocalDateTime.of(dateEnd, LocalTime.MAX));

        //poi将数据写入excel
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("template/test.xlsx");
        XSSFWorkbook excel = new XSSFWorkbook(in);
        XSSFSheet sheet1 = excel.getSheet("Sheet1");
        sheet1.getRow(1).getCell(1).setCellValue("时间:" + dateBegin + "至" + dateEnd);
        XSSFRow row = sheet1.getRow(3);
        row.getCell(2).setCellValue(businessDataVO.getTurnover());
        row.getCell(4).setCellValue(businessDataVO.getOrderCompletionRate());
        row.getCell(6).setCellValue(businessDataVO.getNewUsers());
        row = sheet1.getRow(4);
        row.getCell(2).setCellValue(businessDataVO.getValidOrderCount());
        row.getCell(4).setCellValue(businessDataVO.getUnitPrice());

        for (int i = 0; i < 30; i++){
            LocalDate date = dateBegin.plusDays(i);
            BusinessDataVO businessData = workspaceService.getBusinessData(LocalDateTime.of(date, LocalTime.MIN), LocalDateTime.of(date, LocalTime.MAX));
            row = sheet1.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());
        }
        //将excel下载到客户端浏览器
        ServletOutputStream out = response.getOutputStream();
        excel.write(out);
        in.close();
        out.close();
        excel.close();
    }

你可能感兴趣的:(苍穹外卖,spring,boot,java)