苍穹外卖学习笔记(三十二最终篇)
Apache POI
介绍
Apache POI 是一个处理Miscrosoft Office 各种文件格式的开源项目。简单来说就是,我们可以使用POI在JAVA程序中对Miscrosoft Office各种文件进行读写操作。
一般情况下,POI都是用于操作Excel文件
应用场景:
- 银行网银系统导出交易明细
- 各种业务系统导出Excel报表
- 批量导入业务数据
入门案例
1. 导入maven坐标
<!-- poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi}</version>
</dependency>
2. POITest.java
package com.sky.test;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
* @author Jie.
* @description: TODO
* @date 2024/10/22
* @version: 1.0
*/
public class POITest {
//通过POI创建Excel文件并且写入文件内容
public static void write() throws Exception{
//在内存中创建Excel文件
XSSFWorkbook excel = new XSSFWorkbook();
//在Excel中创建一个Sheet页
XSSFSheet sheet = excel.createSheet("info");
//在Sheet中创建行对象,rownum从0开始
XSSFRow row = sheet.createRow(0);
//在每一行上创建单元格
row.createCell(0).setCellValue("姓名");
row.createCell(1).setCellValue("城市");
row = sheet.createRow(1);
row.createCell(0).setCellValue("张三");
row.createCell(1).setCellValue("北京");
row = sheet.createRow(2);
row.createCell(0).setCellValue("李四");
row.createCell(1).setCellValue("天津");
FileOutputStream out = new FileOutputStream(new File("D:\\info.xlsx"));
excel.write(out);
// 关闭资源
excel.close();
out.close();
}
//通过POI读取Excel文件并且读入文件内容
public static void read() throws Exception{
//读取磁盘上已经存在的excel文件
FileInputStream fileInputStream = new FileInputStream("D:\\info.xlsx");
XSSFWorkbook excel = new XSSFWorkbook(fileInputStream);
XSSFSheet sheet = excel.getSheetAt(0);
int lastRowNum = sheet.getLastRowNum();
for (int i = 1;i <= lastRowNum; i++) {
XSSFRow row = sheet.getRow(i);
String name = row.getCell(0).getStringCellValue();
String name1 = row.getCell(1).getStringCellValue();
System.out.println(name + " " + name1);
}
}
public static void main(String[] args) throws Exception{
write();
read();
}
}
导出运营数据Excel报表
ReportController
@GetMapping("/export")
@ApiOperation("导出Excel报表")
public void export(HttpServletResponse response) {
reportService.exportBusinessData(response);
}
ReportService
/**
* 导出
* @param response 响应
*/
void exportBusinessData(HttpServletResponse response);
ReportServiceImpl
/**
* 导出
*
* @param response 响应
*/
@Override
public void exportBusinessData(HttpServletResponse response) {
//1. 查询数据库,获取营业数据
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文件中
InputStream in = this.getClass().getClassLoader().getResourceAsStream("template/运营数据报表模板.xlsx");
try {
XSSFWorkbook excel = new XSSFWorkbook(in);
//填充营业额数据
XSSFSheet sheet = excel.getSheet("Sheet1");
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.getNewUsers());
}
//3. 通过输出流将Excel文件下载到客户端浏览器
ServletOutputStream out = response.getOutputStream();
excel.write(out);
//4. 关闭资源
out.close();
excel.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
❤️❤️❤️完结撒花❤️❤️❤️