工作台
分析

接口设计:

今日数据:

订单管理:

菜品总览:
、
套餐总览:

代码
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();
}
}