苍穹外卖资源点整理+个人错误解析-Day12-数据统计-EXCEL报表

工作台

分析

接口设计:

今日数据:

订单管理:

菜品总览:

套餐总览:

代码

Apache POI

应用场景:

入门

maven坐标:

通过poi创建excel文件并写数据

代码:

复制代码
public class POItest {
    //通过poi创建excel文件并写入数据
public static void write() throws IOException {
    //在内存中创建一个excel文件
    XSSFWorkbook excel = new XSSFWorkbook();
    //在excel文件中创建一个sheet页.()中是sheef名称
    XSSFSheet sheet = excel.createSheet("info");
    //创建每一行的对象,编号是从0开始,0为第一行
    XSSFRow row = sheet.createRow(1);
    //行上创建单元格
   row.createCell(1).setCellValue("姓名");
    row.createCell(2).setCellValue("城市");
//创建新行
     row=sheet.createRow(2);
    row.createCell(1).setCellValue("aa");
    row.createCell(2).setCellValue("北京");
    //希望在磁盘能看见。使用输出流
    FileOutputStream fileOutputStream = new FileOutputStream("E:\\info.xlsx");
    excel.write(fileOutputStream);
//关闭资源
    fileOutputStream.close();
    excel.close();
}
    public static void main(String[] args) throws IOException {
write();
}


}
复制代码
}

效果如图:

通过poi进行读取已有的ecxcl文件

复制代码
public class POItest {
    //通过poi创建excel文件并写入数据
public static void write() throws IOException {
    //在内存中创建一个excel文件
    XSSFWorkbook excel = new XSSFWorkbook();
    //在excel文件中创建一个sheet页.()中是sheef名称
    XSSFSheet sheet = excel.createSheet("info");
    //创建每一行的对象,编号是从0开始,0为第一行
    XSSFRow row = sheet.createRow(1);
    //行上创建单元格
   row.createCell(1).setCellValue("姓名");
    row.createCell(2).setCellValue("城市");
//创建新行
     row=sheet.createRow(2);
    row.createCell(1).setCellValue("aa");
    row.createCell(2).setCellValue("北京");
    //希望在磁盘能看见。使用输出流
    FileOutputStream fileOutputStream = new FileOutputStream("E:\\info.xlsx");
    excel.write(fileOutputStream);
//关闭资源
    fileOutputStream.close();
    excel.close();
}
public static void read() throws IOException {
    //通过poi读取excel文件
    //读取已经存在的excel文件
    FileInputStream in = new FileInputStream(new File("E:\\info.xlsx"));
    XSSFWorkbook excel = new XSSFWorkbook (in);
    //读取文本内容,先读取第一个sheet页
    XSSFSheet sheet = excel.getSheetAt(0);
    //读取当前页的某一行,需要读取单元格
    //如何读取某一行,因为不知道有多少行,所以先获取最后一个有文字的行号
    int lastRowNum = sheet.getLastRowNum();
    for (int i=1;i<=lastRowNum;i++){
        //获取从上到下某一行
        XSSFRow row = sheet.getRow(i);
        //获取单元格对象
       String cellValue1 = row.getCell(1).getStringCellValue();
        String cellValue2 = row.getCell(2).getStringCellValue();
        System.out.println(cellValue1+""+cellValue2);
    }
    //关闭资源
    excel.close();;
    in.close();

}

导出excel

分析

业务规则:

接口设计:

其中导出形式本质来说是一个文件下载

代码

导出excel实现步骤:

注意,poi是可以创建一个背景色不同,合并单元格,不同字号等的excel,但是代码太过繁琐。一般会自己先创建一个excel模板,然后通过poi进行读取。

controller:

复制代码
@GetMapping("/export")
@ApiOperation("导出运营数据报表")
//导出最近30天,最终表格还需要下载到客户端浏览器,需要输出流
//所以需要传进去一个变量:response
public void export(HttpServletResponse response) throws IOException {
reportService.exportBusinessData(response);

}

serviceimpl:

复制代码
/*
导出运营数据报表
 */
    @Override
    public void exportBusinessData(HttpServletResponse response)  {
//1.查询数据库,获取营业数据,数据是查询最近30天的
        //数据分为概览数据明细数据。概览数据在工作台界面已经查询过了,直接使用方法
        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));

        //2.通过poi将数据写进excel中,需要传一个输入流
        //this.getClass()获取类对象
        //getClassLoader()获得类加载器
        //.getResourceAsStream从某个类路径下面获取资源
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("template/运营数据报表模板");
        //基于模板文件创建一个新的eccel文件
        try {
            XSSFWorkbook excel = new XSSFWorkbook(in);
            //填充数据--时间
            XSSFSheet sheet = excel.getSheet("Shee1");
            sheet.getRow(1).getCell(1).setCellValue("时间:"+dateBegin+"至"+dateEnd);
          //获得第四行
            XSSFRow row = sheet.getRow(3);
            row.getCell(2).setCellValue(businessDataVO.getTurnover());
            row.getCell(4).setCellValue(businessDataVO.getOrderCompletionRate());
            row.getCell(6).setCellValue(businessDataVO.getNewUsers());
            //获得第五行
            row = sheet.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=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.setNewUsers(););
            }
            //3.通过输出流将excel文件下载到客户端浏览器
            ServletOutputStream outputStream = response.getOutputStream();
            excel.write(outputStream);
            //关闭资源
            outputStream.close();
            excel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
相关推荐
开开心心就好2 小时前
免费流畅的远程控制实用工具
linux·运维·服务器·网络·智能手机·excel
!chen5 小时前
可视化Excel文档合并工具
excel
Cloud_Shy61813 小时前
Python 数据分析基础入门:《Excel Python:飞速搞定数据分析与处理》学习笔记系列(第十章 Python 驱动的 Excel 工具 上篇)
vscode·python·数据分析·excel·pandas
Cloud_Shy61813 小时前
Python 数据分析基础入门:《Excel Python:飞速搞定数据分析与处理》学习笔记系列(第十章 Python 驱动的 Excel 工具 下篇)
笔记·python·学习·数据分析·excel·pandas
关中老四15 小时前
不用登录!3 步把 Excel 进度表变成甘特图
excel·项目管理·甘特图·一键生成·进度管理·pjman
Ada大侦探1 天前
新手小白学习数据分析03----Excel 报表之大厂周报(2026最新版实操,包教包会!)
学习·数据分析·excel
软件富二代1 天前
— 批量转换Word题库到Excel的小工具
电脑·word·excel·排版·软件
曦夜日长2 天前
Linux系统篇,开发工具(二):vim的使用与配置
linux·服务器·vim·excel
金玉满堂@bj2 天前
pytest+uiautomation+allure+Excel 数据驱动桌面自动化
自动化·excel·pytest
99乘法口诀万物皆可变2 天前
BMS HIL 自动化测试框架方案(基于 CANoe + C# + Excel)
开发语言·c#·excel