目录
[1.1 工作台是做什么的?](#1.1 工作台是做什么的?)
[1.2 名词解释](#1.2 名词解释)
[1.3 接口设计](#1.3 接口设计)
[1.4 代码实现(核心思路)](#1.4 代码实现(核心思路))
[1.4.1 Controller 层](#1.4.1 Controller 层)
[1.4.2 Service 层:今日数据](#1.4.2 Service 层:今日数据)
[1.4.3 Service 层:订单/菜品/套餐总览](#1.4.3 Service 层:订单/菜品/套餐总览)
[1.4.4 Mapper 层](#1.4.4 Mapper 层)
[1.5 测试](#1.5 测试)
[二、Apache POI:Java 读写 Excel](#二、Apache POI:Java 读写 Excel)
[2.1 POI 是什么?](#2.1 POI 是什么?)
[2.2 写入 Excel 入门](#2.2 写入 Excel 入门)
[2.3 读取 Excel 入门](#2.3 读取 Excel 入门)
[三、导出运营数据 Excel 报表](#三、导出运营数据 Excel 报表)
[3.1 需求分析](#3.1 需求分析)
[3.2 实现步骤](#3.2 实现步骤)
[3.3 代码实现](#3.3 代码实现)
[3.3.1 Controller 层](#3.3.1 Controller 层)
[3.3.2 Service 层](#3.3.2 Service 层)
[3.4 测试](#3.4 测试)
在前面的课程中,我们已经完成了订单、支付、统计图表等核心功能。今天,我们来做两个"收尾但很实用"的功能:工作台数据看板 和运营数据 Excel 导出 。
工作台让商家一登录就能看到今日经营概况;Excel 导出则方便商家存档、分析历史数据。学会这两个功能,你就能掌握后台系统中非常常见的"数据看板 + 文件导出"套路。
一、工作台:商家的数据看板
1.1 工作台是做什么的?

工作台是后台系统的"首页看板",商家登录后能快速看到:
-
今日数据:营业额、有效订单、订单完成率、平均客单价、新增用户
-
订单管理:待接单、待派送、已完成、已取消、全部订单
-
菜品总览:起售菜品数、停售菜品数
-
套餐总览:起售套餐数、停售套餐数
简单理解:工作台就是把商家最关心的数据集中展示在一个页面上。
1.2 名词解释
| 名词 | 含义 |
|---|---|
| 营业额 | 已完成订单的总金额 |
| 有效订单 | 已完成订单的数量 |
| 订单完成率 | 有效订单数 / 总订单数 × 100% |
| 平均客单价 | 营业额 / 有效订单数 |
| 新增用户 | 当日新增用户数量 |
1.3 接口设计
工作台主要包含 4 个新接口:
| 接口 | 路径 | 说明 |
|---|---|---|
| 今日数据 | /admin/workspace/businessData |
查询当天营业数据 |
| 订单管理 | /admin/workspace/overviewOrders |
查询各状态订单数量 |
| 菜品总览 | /admin/workspace/overviewDishes |
查询起售/停售菜品数 |
| 套餐总览 | /admin/workspace/overviewSetmeals |
查询起售/停售套餐数 |
另外两个接口"订单搜索"和"各状态订单数量统计"前面已经完成,这里直接复用。
1.4 代码实现(核心思路)
1.4.1 Controller 层
java
/**
* 工作台
*/
@RestController
@RequestMapping("/admin/workspace")
@Slf4j
@Api(tags = "工作台相关接口")
public class WorkSpaceController {
@Autowired
private WorkspaceService workspaceService;
/**
* 工作台今日数据查询
* @return
*/
@GetMapping("/businessData")
@ApiOperation("工作台今日数据查询")
public Result<BusinessDataVO> businessData(){
//获得当天的开始时间
LocalDateTime begin = LocalDateTime.now().with(LocalTime.MIN);
//获得当天的结束时间
LocalDateTime end = LocalDateTime.now().with(LocalTime.MAX);
BusinessDataVO businessDataVO = workspaceService.getBusinessData(begin, end);
return Result.success(businessDataVO);
}
/**
* 查询订单管理数据
* @return
*/
@GetMapping("/overviewOrders")
@ApiOperation("查询订单管理数据")
public Result<OrderOverViewVO> orderOverView(){
return Result.success(workspaceService.getOrderOverView());
}
/**
* 查询菜品总览
* @return
*/
@GetMapping("/overviewDishes")
@ApiOperation("查询菜品总览")
public Result<DishOverViewVO> dishOverView(){
return Result.success(workspaceService.getDishOverView());
}
/**
* 查询套餐总览
* @return
*/
@GetMapping("/overviewSetmeals")
@ApiOperation("查询套餐总览")
public Result<SetmealOverViewVO> setmealOverView(){
return Result.success(workspaceService.getSetmealOverView());
}
}
1.4.2 Service 层:今日数据
java
public interface WorkspaceService {
/**
* 根据时间段统计营业数据
* @param begin
* @param end
* @return
*/
BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end);
/**
* 查询订单管理数据
* @return
*/
OrderOverViewVO getOrderOverView();
/**
* 查询菜品总览
* @return
*/
DishOverViewVO getDishOverView();
/**
* 查询套餐总览
* @return
*/
SetmealOverViewVO getSetmealOverView();
}
1.4.3 Service 层:订单/菜品/套餐总览
订单管理就是按不同状态分别统计数量:
java
@Service
@Slf4j
public class WorkspaceServiceImpl implements WorkspaceService {
@Autowired
private OrderMapper orderMapper;
@Autowired
private UserMapper userMapper;
@Autowired
private DishMapper dishMapper;
@Autowired
private SetmealMapper setmealMapper;
/**
* 根据时间段统计营业数据
* @param begin
* @param end
* @return
*/
public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
/**
* 营业额:当日已完成订单的总金额
* 有效订单:当日已完成订单的数量
* 订单完成率:有效订单数 / 总订单数
* 平均客单价:营业额 / 有效订单数
* 新增用户:当日新增用户的数量
*/
Map map = new HashMap();
map.put("begin",begin);
map.put("end",end);
//查询总订单数
Integer totalOrderCount = orderMapper.countByMap(map);
map.put("status", Orders.COMPLETED);
//营业额
Double turnover = orderMapper.sumByMap(map);
turnover = turnover == null? 0.0 : turnover;
//有效订单数
Integer validOrderCount = orderMapper.countByMap(map);
Double unitPrice = 0.0;
Double orderCompletionRate = 0.0;
if(totalOrderCount != 0 && validOrderCount != 0){
//订单完成率
orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
//平均客单价
unitPrice = turnover / validOrderCount;
}
//新增用户数
Integer newUsers = userMapper.countByMap(map);
return BusinessDataVO.builder()
.turnover(turnover)
.validOrderCount(validOrderCount)
.orderCompletionRate(orderCompletionRate)
.unitPrice(unitPrice)
.newUsers(newUsers)
.build();
}
/**
* 查询订单管理数据
*
* @return
*/
public OrderOverViewVO getOrderOverView() {
Map map = new HashMap();
map.put("begin", LocalDateTime.now().with(LocalTime.MIN));
map.put("status", Orders.TO_BE_CONFIRMED);
//待接单
Integer waitingOrders = orderMapper.countByMap(map);
//待派送
map.put("status", Orders.CONFIRMED);
Integer deliveredOrders = orderMapper.countByMap(map);
//已完成
map.put("status", Orders.COMPLETED);
Integer completedOrders = orderMapper.countByMap(map);
//已取消
map.put("status", Orders.CANCELLED);
Integer cancelledOrders = orderMapper.countByMap(map);
//全部订单
map.put("status", null);
Integer allOrders = orderMapper.countByMap(map);
return OrderOverViewVO.builder()
.waitingOrders(waitingOrders)
.deliveredOrders(deliveredOrders)
.completedOrders(completedOrders)
.cancelledOrders(cancelledOrders)
.allOrders(allOrders)
.build();
}
/**
* 查询菜品总览
*
* @return
*/
public DishOverViewVO getDishOverView() {
Map map = new HashMap();
map.put("status", StatusConstant.ENABLE);
Integer sold = dishMapper.countByMap(map);
map.put("status", StatusConstant.DISABLE);
Integer discontinued = dishMapper.countByMap(map);
return DishOverViewVO.builder()
.sold(sold)
.discontinued(discontinued)
.build();
}
/**
* 查询套餐总览
*
* @return
*/
public SetmealOverViewVO getSetmealOverView() {
Map map = new HashMap();
map.put("status", StatusConstant.ENABLE);
Integer sold = setmealMapper.countByMap(map);
map.put("status", StatusConstant.DISABLE);
Integer discontinued = setmealMapper.countByMap(map);
return SetmealOverViewVO.builder()
.sold(sold)
.discontinued(discontinued)
.build();
}
}
菜品总览和套餐总览类似,按 status 分别统计起售和停售数量。
1.4.4 Mapper 层
为 DishMapper、SetmealMapper 增加 countByMap:
java
/**
* 根据条件统计套餐数量
* @param map
* @return
*/
Integer countByMap(Map map);
java
<select id="countByMap" resultType="java.lang.Integer">
select count(id) from setmeal
<where>
<if test="status != null">
and status = #{status}
</if>
<if test="categoryId != null">
and category_id = #{categoryId}
</if>
</where>
</select>
套餐的 SQL 同理,把表名换成 setmeal 即可。
1.5 测试
启动项目,用 Swagger 或前后端联调测试:

-
今日数据:能看到营业额、有效订单、完成率、客单价、新增用户
-
订单管理:能看到待接单、待派送、已完成、已取消、全部订单
-
菜品/套餐总览:能看到起售和停售数量
二、Apache POI:Java 读写 Excel
2.1 POI 是什么?

Apache POI 是一个开源项目,可以用 Java 读写 Microsoft Office 文件,最常用的是 Excel。
常见应用场景:
-
银行导出交易明细
-
业务系统导出报表
-
批量导入数据
Maven 坐标(项目中已引入):
java
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.16</version>
</dependency>
2.2 写入 Excel 入门
核心 API:
-
XSSFWorkbook:Excel 文件对象 -
XSSFSheet:Sheet 页 -
XSSFRow:行 -
XSSFCell:单元格
java
public class POITest {
/**
* 基于POI向Excel文件写入数据
* @throws Exception
*/
public static void write() throws Exception{
//在内存中创建一个Excel文件对象
XSSFWorkbook excel = new XSSFWorkbook();
//创建Sheet页
XSSFSheet sheet = excel.createSheet("itcast");
//在Sheet页中创建行,0表示第1行
XSSFRow row1 = sheet.createRow(0);
//创建单元格并在单元格中设置值,单元格编号也是从0开始,1表示第2个单元格
row1.createCell(1).setCellValue("姓名");
row1.createCell(2).setCellValue("城市");
XSSFRow row2 = sheet.createRow(1);
row2.createCell(1).setCellValue("张三");
row2.createCell(2).setCellValue("北京");
XSSFRow row3 = sheet.createRow(2);
row3.createCell(1).setCellValue("李四");
row3.createCell(2).setCellValue("上海");
FileOutputStream out = new FileOutputStream(new File("D:\\itcast.xlsx"));
//通过输出流将内存中的Excel文件写入到磁盘上
excel.write(out);
//关闭资源
out.flush();
out.close();
excel.close();
}
public static void main(String[] args) throws Exception {
write();
}
}

注意:行和单元格编号都从
0开始。
2.3 读取 Excel 入门
java
public class POITest {
/**
* 基于POI读取Excel文件
* @throws Exception
*/
public static void read() throws Exception{
FileInputStream in = new FileInputStream(new File("D:\\itcast.xlsx"));
//通过输入流读取指定的Excel文件
XSSFWorkbook excel = new XSSFWorkbook(in);
//获取Excel文件的第1个Sheet页
XSSFSheet sheet = excel.getSheetAt(0);
//获取Sheet页中的最后一行的行号
int lastRowNum = sheet.getLastRowNum();
for (int i = 0; i <= lastRowNum; i++) {
//获取Sheet页中的行
XSSFRow titleRow = sheet.getRow(i);
//获取行的第2个单元格
XSSFCell cell1 = titleRow.getCell(1);
//获取单元格中的文本内容
String cellValue1 = cell1.getStringCellValue();
//获取行的第3个单元格
XSSFCell cell2 = titleRow.getCell(2);
//获取单元格中的文本内容
String cellValue2 = cell2.getStringCellValue();
System.out.println(cellValue1 + " " +cellValue2);
}
//关闭资源
in.close();
excel.close();
}
public static void main(String[] args) throws Exception {
read();
}
}

三、导出运营数据 Excel 报表
3.1 需求分析

在数据统计页面点击"数据导出",下载一个 Excel 文件,内容包括:

-
概览数据:时间区间、营业额、有效订单、完成率、平均客单价、新增用户
-
明细数据:最近 30 天每天的营业额、有效订单、完成率、客单价、新增用户
接口设计:
-
请求方式:
GET -
请求路径:
/admin/report/export -
无请求参数(后端自动计算最近 30 天)
-
无 JSON 返回(通过输出流下载文件)
3.2 实现步骤

-
准备 Excel 模板文件
-
查询近 30 天运营数据
-
将数据写入模板
-
通过输出流下载到浏览器
模板文件放在
resources/template/运营数据报表模板.xlsx。
3.3 代码实现
3.3.1 Controller 层
java
/**
* 导出运营数据报表
* @param response
*/
@GetMapping("/export")
@ApiOperation("导出运营数据报表")
public void export(HttpServletResponse response){
reportService.exportBusinessData(response);
}
3.3.2 Service 层
java
/**
* 导出近30天的运营数据报表
* @param response
**/
void exportBusinessData(HttpServletResponse response);
java
/**导出近30天的运营数据报表
* @param response
**/
public void exportBusinessData(HttpServletResponse response) {
LocalDate begin = LocalDate.now().minusDays(30);
LocalDate end = LocalDate.now().minusDays(1);
//查询概览运营数据,提供给Excel模板文件
BusinessDataVO businessData = workspaceService.getBusinessData(LocalDateTime.of(begin,LocalTime.MIN), LocalDateTime.of(end, LocalTime.MAX));
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("template/运营数据报表模板.xlsx");
try {
//基于提供好的模板文件创建一个新的Excel表格对象
XSSFWorkbook excel = new XSSFWorkbook(inputStream);
//获得Excel文件中的一个Sheet页
XSSFSheet sheet = excel.getSheet("Sheet1");
sheet.getRow(1).getCell(1).setCellValue(begin + "至" + end);
//获得第4行
XSSFRow row = sheet.getRow(3);
//获取单元格
row.getCell(2).setCellValue(businessData.getTurnover());
row.getCell(4).setCellValue(businessData.getOrderCompletionRate());
row.getCell(6).setCellValue(businessData.getNewUsers());
row = sheet.getRow(4);
row.getCell(2).setCellValue(businessData.getValidOrderCount());
row.getCell(4).setCellValue(businessData.getUnitPrice());
for (int i = 0; i < 30; i++) {
LocalDate date = begin.plusDays(i);
//准备明细数据
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.getNewUsers());
}
//通过输出流将文件下载到客户端浏览器中
ServletOutputStream out = response.getOutputStream();
excel.write(out);
//关闭资源
out.flush();
out.close();
excel.close();
}catch (IOException e){
e.printStackTrace();
}
}
3.4 测试
前后端联调:
-
进入数据统计页面

-
点击"数据导出"
-
浏览器下载 Excel 文件
-
打开文件,检查概览数据和 30 天明细是否正确

四、总结
| 功能 | 核心知识点 |
|---|---|
| 工作台 | 按时间区间统计营业数据;按状态统计订单、菜品、套餐数量 |
| Apache POI | XSSFWorkbook、XSSFSheet、XSSFRow、XSSFCell 读写 Excel |
| Excel 导出 | 读取模板 → 查询数据 → 填充单元格 → 输出流下载 |