苍穹外卖资源点整理+个人错误解析-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();
        }

    }
相关推荐
开开心心就好1 小时前
免费好用:PPT演示计时提醒工具
windows·计算机视觉·计算机外设·逻辑回归·excel·深度优先·csdn开发云
NocoBase9 小时前
为 Excel 数据快速构建 Web 应用:4 种方法对比
前端·人工智能·低代码·开源·excel
fengyehongWorld9 小时前
Excel 照相机功能
excel
yuhulkjv33511 小时前
豆包导出的Excel公式失效
人工智能·ai·chatgpt·excel·豆包·deepseek·ai导出鸭
诸葛大钢铁1 天前
Java实现Excel文件合并
java·windows·excel
Ada大侦探1 天前
Excel实现某一列特定选项进行筛选且配上特定颜色
excel
开开心心就好2 天前
桌面图标乱了怎么办,一键恢复固定位置工具
运维·服务器·windows·pdf·excel·3dsmax·houdini
偷心伊普西隆2 天前
EXCEL 自动化链接更新工具设计方案
自动化·excel
ai_coder_ai2 天前
如何在自动化脚本中使用excel文件?
excel·autojs·自动化脚本·冰狐智能辅助·easyclick
前端程序猿i2 天前
纯JS 导出 Excel 工具
开发语言·javascript·excel