外卖开发(九)——Excel数据报表ApachePOI

外卖开发(九)------Excel数据报表

一、ApachePOI

Apache POl是一个处理Miscrosoft Office各种文件格式的开源项目。简单来说就是,我们可以使用POI在Java程序中对Miscrosoft Office各种文件进行读写操作。

一般情况下,POI都是用于操作 Excel文件。

maven坐标

bash 复制代码
<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>

二、入门案例

将数据写入Excel

java 复制代码
//在内存中创建一个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();

读取Excel中的数据:

java 复制代码
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();

运行效果:

三、导出运营数据报表





1、ReportController

java 复制代码
/**
     * 导出数据报表
     * @param response
     */
    @GetMapping("/export")
    @ApiOperation("到处数据报表")
    public void export(HttpServletResponse response){  //传递response,以便使用输出流
        workSpaceService.exportData(response);
    }

2、ReportService

java 复制代码
 /**
     * 导出运营数据报表
     * @param response
     */
    @Override
    public void exportData(HttpServletResponse response){
        //查询数据库,获取数据--查询最近30天
        LocalDate dateBegin = LocalDate.now().minusDays(30);
        LocalDate dateEnd = LocalDate.now().minusDays(1);
        LocalDateTime begin = LocalDateTime.of(dateBegin, LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(dateEnd, LocalTime.MAX);
        BusinessDataVO businessDataVO = getbusinessDataMoreDay(begin,end);
        //通过poi写入excel
        //获得输入流
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("template/template.xlsx");
        try {
            XSSFWorkbook excel = new XSSFWorkbook(in);
            //填充数据
            XSSFSheet sheet = excel.getSheet("Sheet1");
            sheet.getRow(1).getCell(1).setCellValue("时间 "+ dateBegin+" 至 "+dateEnd);

            sheet.getRow(3).getCell(2).setCellValue(businessDataVO.getTurnover());
            sheet.getRow(3).getCell(4).setCellValue(businessDataVO.getOrderCompletionRate());
            sheet.getRow(3).getCell(6).setCellValue(businessDataVO.getNewUsers());

            sheet.getRow(4).getCell(2).setCellValue(businessDataVO.getValidOrderCount());
            sheet.getRow(4).getCell(4).setCellValue(businessDataVO.getUnitPrice());

            //填充明细数据(30天,每天的明细)
            List<LocalDate> dateList = new ArrayList<>();//30天每天的日期,用于循环查询数据库
            dateList.add(dateBegin);
            while(!dateBegin.isEqual(dateEnd)){
                dateBegin = dateBegin.plusDays(1);
                dateList.add(dateBegin);
            }
            //运营数据集合,每一天运营数据都是一个集合项
            List<BusinessDataVO> businessDataList = new ArrayList<>();
            for (LocalDate date : dateList){
                LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
                LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
                BusinessDataVO businessData = getbusinessDataMoreDay(beginTime, endTime);
                businessDataList.add(businessData);
            }
            //POI装入excel
            for(int i =7; i< (businessDataList.size()+7); i++){
                XSSFRow row = sheet.getRow(i);
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                String formattedDate = dateList.get(i-7).format(formatter);
                row.getCell(1).setCellValue(formattedDate);
                row.getCell(2).setCellValue(businessDataList.get(i-7).getTurnover());
                row.getCell(3).setCellValue(businessDataList.get(i-7).getValidOrderCount());
                row.getCell(4).setCellValue(businessDataList.get(i-7).getOrderCompletionRate());
                row.getCell(5).setCellValue(businessDataList.get(i-7).getUnitPrice());
                row.getCell(6).setCellValue(businessDataList.get(i-7).getNewUsers());
            }

            //通过输出流,把excel下载到浏览器
            ServletOutputStream out = response.getOutputStream();
            excel.write(out);

            //关闭资源
            out.close();
            excel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
相关推荐
xiaopai94516 小时前
采购系统和Excel表格区别:钢结构项目物资管理的真实较量
excel·建米软件·采购系统
协享科技17 小时前
2026 年 AI 表格工具怎么选:ChatGPT、原生 Copilot/Gemini 与公式助手
人工智能·chatgpt·excel·copilot
用户298698530141 天前
前端实战:在 React 中使用 JavaScript 一键导出 Excel 图表与形状为图片
javascript·react.js·excel
Q一件事1 天前
arcpy实现分区统计到一张excel中
excel
AI刀刀1 天前
deepseek 内容粘贴后符号丢失怎么办?AI 导出鸭实测解决排版乱码问题
android·人工智能·excel·ai导出鸭
圣光SG2 天前
Excel 函数大全
算法·excel
小小的木头人3 天前
Python 批量解析 Excel 经纬度,调用高德地图 API 获取中文地址
开发语言·python·excel
观远数据3 天前
从离线开发到实时同步:DataFlow如何支撑企业级数据治理闭环
java·windows·microsoft·excel
tianyatest3 天前
表格分类统计及排序
java·excel·暖通
维C°3 天前
PowerBI数据准备—合并文件夹中同表头Excel或csv
excel·powerbi