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

    }
相关推荐
LAM LAB4 天前
【VBA】Excel指定单元格范围内字体设置样式,处理导出课表单元格
excel·vba
在这habit之下4 天前
Keepalived学习总结
excel
Youngchatgpt4 天前
如何在 Excel 中使用 ChatGPT:自动化任务和编写公式
人工智能·chatgpt·自动化·excel
开开心心就好4 天前
安卓开源应用,超时提醒紧急人护独居安全
windows·决策树·计算机视觉·pdf·计算机外设·excel·动态规划
D_C_tyu4 天前
Vue3 + Element Plus | el-table 多级表头表格导出 Excel(含合并单元格、单元格居中)第二版
vue.js·elementui·excel
骆驼爱记录4 天前
WPS页码设置:第X页共Y-1页
自动化·word·excel·wps·新人首发
Cxiaomu5 天前
Python 文件解析: Excel / Word / PDF 的解析、处理、预览与下载
python·word·excel
2501_930707785 天前
如何使用C#代码从 PDF 中提取表格并另存为Excel文件
pdf·excel
pacong5 天前
B生所学EXCEL
人工智能·excel