二維碼圖片個人網(wǎng)站seo入門
一、工作臺
聯(lián)系昨天
要實現(xiàn)的功能和昨天差不多,都是查詢數(shù)據(jù)。
所以我們就寫出查詢語句,然后直接導(dǎo)入已經(jīng)寫好的代碼。
實現(xiàn)效果
查詢語句
今日數(shù)據(jù)
營業(yè)額
select count(amount) from orders
where status=5 and order_time >= #{begin} and order_time <= #{end}
有效訂單
select count(*) from orders
where status=5 and order_time >= #{begin} and order_time <= #{end}
訂單完成率
所有訂單:
select count(*) from orders
where order_time >= #{begin} and order_time <= #{end}
訂單完成率 = 有效訂單 / 所有訂單
平均客單價
平均客單價 = 營業(yè)額 / 有效訂單
新增用戶數(shù)
select count(*) from user
where create_time >= #{begin} and create_time <= #{end}
訂單數(shù)據(jù)
待接單
select count(*) from orders
where status=2 and order_time >=#{begin} and order_time <= #{end}
待派送
select count(*) from orders
where status=3?and order_time >=#{begin} and order_time <= #{end}
已完成
select count(*) from orders
where status=5?and order_time >=#{begin} and order_time <= #{end}
已取消
select count(*) from orders
where status=6?and order_time >=#{begin} and order_time <= #{end}
全部訂單
select count(*) from orders
where order_time >=#{begin} and order_time <= #{end}
菜品總覽
起售菜品
select count(*) from dish
where status=1
停售菜品
select count(*) from dish
where status=1
套餐總覽
起售套餐
select count(*) from setmeal
where status=1
停售套餐
select count(*) from setmeal
where status=0
導(dǎo)入代碼
下載好黑馬該項目的資料:
然后自己導(dǎo)入。
二、Apache POI
介紹
操作Office文件的包。本文該項目中主要用來讀寫excel表。
應(yīng)用場景主要在:交易明細(xì)、銷量統(tǒng)計、批量數(shù)據(jù)導(dǎo)入(批量添加)
寫入Excel
步驟
-
先創(chuàng)建Excel文檔/工作簿
-
在工作簿中創(chuàng)建表格
-
在表格中創(chuàng)建行
-
在行中創(chuàng)建單元格
-
往單元格中設(shè)置數(shù)據(jù)
-
將整個Excel文檔寫到硬盤上
代碼
直接在測試類中寫例子的測試的。
@Test
public void testWrite() throws IOException {// 1. 創(chuàng)建整個工作簿XSSFWorkbook workbook = new XSSFWorkbook();// 2. 在工作簿中創(chuàng)建表格XSSFSheet sheet1 = workbook.createSheet("表格1");// 3. 在表格中創(chuàng)建行XSSFRow row_1 = sheet1.createRow(1);// 4. 在行中創(chuàng)建單元格XSSFCell cell_1_0 = row_1.createCell(0);// 5. 網(wǎng)單元格中設(shè)置數(shù)據(jù)cell_1_0.setCellValue("哈哈,我是cell_1_0");// 6. 將整個Excel文檔寫到硬盤上FileOutputStream fos = new FileOutputStream("D:/a.xlsx");workbook.write(fos);// 7. 釋放資源fos.close();workbook.close();
}
讀出Excel
步驟
-
先創(chuàng)建工作簿,關(guān)聯(lián)本地Excel文檔
-
從工作簿中獲取表格
-
從表格中獲取行
-
從行中獲取單元格
-
從單元格中獲取數(shù)據(jù)
代碼
@Test
public void testRead() throws IOException {// 1. 先創(chuàng)建工作簿,關(guān)聯(lián)本地Excel文檔XSSFWorkbook workbook = new XSSFWorkbook("D:/a.xlsx");// 2. 從工作簿中獲取表格XSSFSheet sheet = workbook.getSheetAt(0);// 3. 從表格中獲取行XSSFRow row4 = sheet.getRow(3);// 4. 從行中獲取單元格 以及 5. 從單元格中獲取數(shù)據(jù)String name = row4.getCell(0).getStringCellValue();String age = row4.getCell(1).getStringCellValue();System.out.println(name);System.out.println(age);XSSFRow row5 = sheet.getRow(4);System.out.println(row5.getCell(0).getStringCellValue());System.out.println(row5.getCell(1).getNumericCellValue());workbook.close();
}
三、導(dǎo)出運營數(shù)據(jù)
需求分析
導(dǎo)出近30天的運營數(shù)據(jù)。
步驟
-
讀取Excel模版到內(nèi)存中。
-
準(zhǔn)備運營數(shù)據(jù)
-
將數(shù)據(jù)寫到Excel模板中。
-
將Excel文檔響應(yīng)回瀏覽器(文件下載)
代碼
Controller:
@GetMapping("/export")
@ApiOperation("導(dǎo)出運營數(shù)據(jù)報表")
public String export(HttpServletResponse response) throws IOException {reportService.exportBusinessData(response);return "OK";
}
Service:
@Override
public void exportBusinessData(HttpServletResponse response) throws IOException{InputStream is = ClassLoader.getSystemResourceAsStream("運營數(shù)據(jù)報表模板.xlsx");XSSFWorkbook workbook = new XSSFWorkbook(is);LocalDate begin = LocalDate.now().plusDays(-30);LocalDate end = LocalDate.now().plusDays(-1);BusinessDataVO businessDataVO = workspaceService.getBusinessData(LocalDateTime.of(begin, LocalTime.MIN),LocalDateTime.of(end, LocalTime.MAX));XSSFSheet sheet = workbook.getSheetAt(0);sheet.getRow(1).getCell(1).setCellValue("時間:" + begin + "至" + end);XSSFRow row4 = sheet.getRow(3);row4.getCell(2).setCellValue(businessDataVO.getTurnover());row4.getCell(4).setCellValue(businessDataVO.getOrderCompletionRate());row4.getCell(6).setCellValue(businessDataVO.getNewUsers());XSSFRow row5 = sheet.getRow(4);row5.getCell(2).setCellValue(businessDataVO.getValidOrderCount());row5.getCell(4).setCellValue(businessDataVO.getUnitPrice());int i = 0;while (begin.compareTo(end) <= 0) {BusinessDataVO businessData = workspaceService.getBusinessData(LocalDateTime.of(begin, LocalTime.MIN),LocalDateTime.of(begin, LocalTime.MAX));XSSFRow row = sheet.getRow(7 + i++);row.getCell(1).setCellValue(begin.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());begin = begin.plusDays(1);}workbook.write(response.getOutputStream());}
注意的點
ClassLoader能加載的文件位置
ClassLoader能加載的文件位置在resources下。
放入resources后需要的操作
需要用maven構(gòu)建管理的complie編譯一下,才能保證類加載器ClassLoader加載到。
創(chuàng)建的POI與Office對應(yīng)的下標(biāo)
下標(biāo)中g(shù)etRow(0)與getCell(1)對應(yīng)的分別是第一列第2行的數(shù)據(jù)。