当前位置: 首页 > news >正文

苍穹外卖集成 Apache POI Java实现Excel文件的读写下载

苍穹外卖 day12 Echats 营业台数据可视化整合_软工菜鸡的博客-CSDN博客

Apache POI - the Java API for Microsoft Documents
Project News
16 September 2022 - POI 5.2.3 available
The Apache POI team is pleased to announce the release of 5.2.3. Several dependencies were updated to their latest versions to pick up security fixes and other improvements.

A summary of changes is available in the Release Notes. A full list of changes is available in the change log. People interested should also follow the dev list to track progress.

See the downloads page for more details.

POI requires Java 8 or newer since version 4.0.1.

2. Apache POI

2.1 介绍

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

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

Apache POI 的应用场景

  • 银行网银系统导出交易明细

  • 各种业务系统导出Excel报表

  • 批量导入业务数据

2.2 入门案例

Apache POI既可以将数据写入Excel文件,也可以读取Excel文件中的数据,接下来分别进行实现。

Apache POI的maven坐标:(项目中已导入)

<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.1 将数据写入Excel文件

1). 代码开发

package com.sky.test;import org.apache.poi.xssf.usermodel.XSSFCell;
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;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();}
}

2). 实现效果

在D盘中生成itcast.xlsx文件,创建名称为itcast的Sheet页,同时将内容成功写入。

2.2.2 读取Excel文件中的数据

1). 代码开发

package com.sky.test;import org.apache.poi.xssf.usermodel.XSSFCell;
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;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();}
}

2). 实现效果

将itcast.xlsx文件中的数据进行读取

3. 导出运营数据Excel报表

3.1 需求分析和设计

3.1.1 产品原型

在数据统计页面,有一个数据导出的按钮,点击该按钮时,其实就会下载一个文件。这个文件实际上是一个Excel形式的文件,文件中主要包含最近30日运营相关的数据。表格的形式已经固定,主要由概览数据和明细数据两部分组成。真正导出这个报表之后,相对应的数字就会填充在表格中,就可以进行存档。

原型图:

导出的Excel报表格式:

业务规则:

  • 导出Excel形式的报表文件
  • 导出最近30天的运营数据

3.1.2 接口设计

通过上述原型图设计对应的接口。

注意:

  • 当前接口没有传递参数,因为导出的是最近30天的运营数据,后端计算即可,所以不需要任何参数
  • 当前接口没有返回数据,因为报表导出功能本质上是文件下载,服务端会通过输出流将Excel文件下载到客户端浏览器

3.2 代码开发

3.2.1 实现步骤

1). 设计Excel模板文件

2). 查询近30天的运营数据

3). 将查询到的运营数据写入模板文件

4). 通过输出流将Excel文件下载到客户端浏览器

3.2.2 Controller层

根据接口定义,在ReportController中创建export方法:

    /*** 导出运营数据报表* @param response*/@GetMapping("/export")@ApiOperation("导出运营数据报表")public void export(HttpServletResponse response){reportService.exportBusinessData(response);}

3.2.3 Service层接口

在ReportService接口中声明导出运营数据报表的方法:

	/*** 导出近30天的运营数据报表* @param response**/void exportBusinessData(HttpServletResponse response);

3.2.4 Service层实现类

在ReportServiceImpl实现类中实现导出运营数据报表的方法:

提前将资料中的运营数据报表模板.xlsx拷贝到项目的resources/template目录中

    /**导出近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.3 功能测试

直接使用前后端联调测试。

进入数据统计

点击数据导出:Excel报表下载成功

3.4 代码提交

后续步骤和其它功能代码提交一致,不再赘述。

非常感谢您阅读到这里,创作不易!如果这篇文章对您有帮助,希望能留下您的点赞👍 关注💖 收藏 💕评论💬感谢支持!!!

听说 三连能够给人 带来好运!更有可能年入百w,进入大厂,上岸

http://www.lryc.cn/news/165421.html

相关文章:

  • iOS逆向:工具安装
  • 十种数据库缓存相关的技术和机制
  • 【C++】封装unordered_map和unordered_set(用哈希桶实现)
  • 面试问题回忆
  • 更多场景、更多选择,Milvus 新消息队列 NATS 了解一下
  • 如何通过python实现一个web自动化测试框架?
  • Linux —— 信号阻塞
  • 【【萌新编写riscV之计算机体系结构之CPU 总二】】
  • error:03000086:digital envelope routines::initialization error
  • 暴涨130万粉仅用3个月,一招转型成B站热门UP主
  • 【Linus】vim的使用:命令模式、底行模式、插入模式、视图模式、替换模式的常用操作介绍
  • leetcode第362场周赛补题
  • SpringMvc 之crud增删改查应用
  • 【业务功能109】微服务-springcloud-springboot-Skywalking-链路追踪-监控
  • 《向量数据库指南》——AI原生向量数据库Milvus Cloud 2.3架构升级
  • Flutter中实现交互式Webview的方法
  • 【Java Web】用Redis优化登陆模块
  • 华为云云耀云服务器L实例评测|docker私有仓库部署手册
  • JAVA-3DES对称加解密工具(不依赖第三方库)
  • 基于Matlab卡尔曼滤波的IMU和GPS组合导航数据融合(附上源码+数据)
  • net自动排课系统完整源码(适合智慧校园)
  • Matlab匿名函数教程
  • 【Vue】一文让你进入Vue的大门
  • Linux mmap读/写触发共享文件页生命周期
  • linux 用户、组操作
  • MySQL报错this is incompatible withsal mode=only full group by处理办法
  • Mybatis 动态语言 - mybatis-freemarker
  • 软件源码开发,网络中的“摄像头”:运维监控系统
  • ping命令
  • MFC:程序的托盘显示