前言
工作臺(tái)用于展示各項(xiàng)運(yùn)營(yíng)數(shù)據(jù),在圖形報(bào)表中很多方法都已實(shí)現(xiàn),這部分的業(yè)務(wù)邏輯也差不多,只是返回的數(shù)據(jù)類型不一樣。
POI用于操作 Excel 文件,可以對(duì)Excel文件進(jìn)行讀寫操作
導(dǎo)出運(yùn)營(yíng)數(shù)據(jù)報(bào)表首先就是查詢近30天的運(yùn)營(yíng)數(shù)據(jù),然后通過POI將查詢到的運(yùn)營(yíng)數(shù)據(jù)寫入模板文件
最后通過輸出流將Excel文件下載到客戶端瀏覽器。
一、工作臺(tái)
工作臺(tái)是系統(tǒng)運(yùn)營(yíng)的數(shù)據(jù)看板,并提供快捷操作入口,可以有效提高商家的工作效率。
展示的數(shù)據(jù)包括:今日數(shù)據(jù)、訂單管理、菜品總覽、套餐總覽、訂單信息(已完成)
這里只展示今日數(shù)據(jù)的代碼,其他部分邏輯差不多,前面的業(yè)務(wù)邏輯中也已
對(duì)相關(guān)sql語句編寫過了,整體的業(yè)務(wù)邏輯都是根據(jù)需要返回的數(shù)據(jù)去計(jì)算對(duì)應(yīng)的值,然后封裝到VO實(shí)體類中返回給前端。
1.1 今日數(shù)據(jù)
1.1.1 接口設(shè)計(jì)
1.1.2 代碼實(shí)現(xiàn)
1、在workspaceController中定義方法,實(shí)現(xiàn)工作臺(tái)展示今日數(shù)據(jù)
@GetMapping("/businessData")
@ApiOperation("今日數(shù)據(jù)")
public Result<BusinessDataVO> businessData(){
//獲得當(dāng)天的開始時(shí)間
LocalDateTime begin = LocalDateTime.now().with(LocalTime.MIN);
LocalDateTime end = LocalDateTime.now().with(LocalTime.MAX);
BusinessDataVO businessDataVO = workspaceService.businessData(begin,end);
return Result.success(businessDataVO);
}
2、在WorkspaceService中編寫businessData方法,在WorkspaceServiceImpl中實(shí)現(xiàn)
public BusinessDataVO businessData(LocalDateTime begin, LocalDateTime end) {
Map map = new HashMap();
map.put("begin",begin);
map.put("end",end);
//查詢總訂單數(shù)
Integer totalOrderCount = orderMapper.countByMap(map);
map.put("status", Orders.COMPLETED);
//營(yíng)業(yè)額
Double turnover = orderMapper.sumByMap(map);
turnover = turnover == null? 0.0 : turnover;
//有效訂單數(shù)
Integer validOrderCount = orderMapper.countByMap(map);
Double unitPrice = 0.0;
Double orderCompletionRate = 0.0;
if(totalOrderCount != 0 && validOrderCount != 0){
//訂單完成率
orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
//平均客單價(jià)
unitPrice = turnover / validOrderCount;
}
//新增用戶數(shù)
Integer newUsers = userMapper.countByMap(map);
return BusinessDataVO.builder()
.turnover(turnover)
.validOrderCount(validOrderCount)
.orderCompletionRate(orderCompletionRate)
.unitPrice(unitPrice)
.newUsers(newUsers)
.build();
}
3、在xxxMapper中定義方法,在day11統(tǒng)計(jì)報(bào)表中很多方法都已實(shí)現(xiàn)。
1.2 訂單管理接口
1.3菜品總覽接口
1.4 套餐總覽接口
1.5 訂單搜索(已完成)
1.6 各個(gè)狀態(tài)的訂單數(shù)量統(tǒng)計(jì)(已完成)
二、Apache POI
2.1 概述
Apache POI 是一個(gè)處理Miscrosoft Office各種文件格式的開源項(xiàng)目。簡(jiǎn)單來說就是,我們可以使用 POI 在 Java 程序中對(duì)Miscrosoft Office各種文件進(jìn)行讀寫操作。
一般情況下,POI 都是用于操作 Excel 文件。
應(yīng)用場(chǎng)景:
- 銀行網(wǎng)銀系統(tǒng)導(dǎo)出交易明細(xì)
- 各種業(yè)務(wù)系統(tǒng)導(dǎo)出Excel報(bào)表
- 批量導(dǎo)入業(yè)務(wù)數(shù)據(jù)
2.2 效果展示
三、導(dǎo)出運(yùn)營(yíng)數(shù)據(jù)Excel報(bào)表
3.1 業(yè)務(wù)規(guī)則
導(dǎo)出Excel形式的報(bào)表文件
導(dǎo)出最近30天的運(yùn)營(yíng)數(shù)據(jù)
3.2 接口設(shè)計(jì)
當(dāng)前接口沒有返回?cái)?shù)據(jù),因?yàn)閳?bào)表導(dǎo)出功能本質(zhì)上是文件下載,
服務(wù)端會(huì)通過輸出流將Excel文件下載到客戶端瀏覽器
3.3 代碼實(shí)現(xiàn)
1、設(shè)計(jì)Excel模板文件
2、在ReportController中創(chuàng)建export方法文章來源:http://www.zghlxwxcb.cn/news/detail-856952.html
@GetMapping("/export")
@ApiOperation("到處運(yùn)營(yíng)數(shù)據(jù)報(bào)表")
public void export(HttpServletResponse response){
reportService.exportBusinessData(response);
}
3、在ReportService接口中聲明導(dǎo)出運(yùn)營(yíng)數(shù)據(jù)報(bào)表的方法,在ReportServiceImpl實(shí)現(xiàn)類中實(shí)現(xiàn)文章來源地址http://www.zghlxwxcb.cn/news/detail-856952.html
public void exportBusinessData(HttpServletResponse response) {
//1. 查詢數(shù)據(jù)庫(kù),獲取營(yíng)業(yè)數(shù)據(jù)--獲取最近30天的運(yùn)營(yíng)數(shù)據(jù)
LocalDate dateBegin = LocalDate.now().minusDays(30);
LocalDate dateEnd = LocalDate.now().minusDays(1);
//查詢今日數(shù)據(jù)
BusinessDataVO businessDataVO = workspaceService.businessData(LocalDateTime.of(dateBegin, LocalTime.MIN), LocalDateTime.of(dateEnd, LocalTime.MAX));
//2. 通過POI將數(shù)據(jù)寫入到excel文件中
InputStream in = this.getClass().getClassLoader().getResourceAsStream("template/運(yùn)營(yíng)數(shù)據(jù)報(bào)表模板.xlsx");
try {
//基于模板文件創(chuàng)建一個(gè)新的Excel文件
XSSFWorkbook excel = new XSSFWorkbook(in);
//獲取表格文件sheet頁
XSSFSheet sheet = excel.getSheet("Sheet1");
//填充數(shù)據(jù)--時(shí)間
sheet.getRow(1).getCell(1).setCellValue("時(shí)間:"+dateBegin+"至"+dateEnd);
//獲得第4行
XSSFRow row = sheet.getRow(3);
row.getCell(2).setCellValue(businessDataVO.getTurnover());
row.getCell(4).setCellValue(businessDataVO.getOrderCompletionRate());
row.getCell(6).setCellValue(businessDataVO.getNewUsers());
//獲得第5行
row = sheet.getRow(4);
row.getCell(2).setCellValue(businessDataVO.getValidOrderCount());
row.getCell(4).setCellValue(businessDataVO.getUnitPrice());
//填充明細(xì)數(shù)據(jù)
for (int i = 0; i < 30; i++) {
LocalDate date = dateBegin.plusDays(i);
BusinessDataVO businessData = workspaceService.businessData(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.getNewUsers());
}
//3. 通過輸出流將Excel文件下載到客戶端瀏覽器
ServletOutputStream out = response.getOutputStream();
excel.write(out);
//4.關(guān)閉資源
out.close();
excel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
到了這里,關(guān)于蒼穹外賣day12 (Apache POI) 數(shù)據(jù)統(tǒng)計(jì)-Excel報(bào)表的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!