Apache POI及easyExcel读取及写入excel文件

目录

1.excel

2.使用场景

3.Apache POI

4.easyExcel

5.总结


1.excel

excel分为两版,03版和07版。

03版的后缀为xls,最大有65536行。

07版的后缀为xlsx,最大行数没有限制。

2.使用场景

将用户信息导出到excel表格中。

将excel中的数据读取到数据库中。

3.Apache POI

(1)说明

Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。

HSSF - 提供读写MicrostExcel格式档案的功能(excel 03)。
XSSF - 提供读写MicrosofExcel OOXML各式档案的功能(excel 07)。
HWPF -提供读写Microsoft Word洛式档案的功能。
HSLF - 提供读写Microsof PowerPoint式档案的功能
HDGF - 提供读写Microsoft Visio各式档案的功能

使用起来比较麻烦,而且数据量大时会出现OOM异常。

使用poi读取不同格式的excel文件的使用jar包是不同的。如下:

        <!--        xls-03--><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.1.2</version></dependency><!--        xls-07--><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.1.2</version></dependency>

(2)读取说明

将excel分为4个对象,excel文件是一个工作簿;excel中有好多sheet,这个是工作表;表中有行,行中有列。使用apache poi读取或写入文件,就是先获取工作簿,然后获取工作表,再获取某行,最后获取行中某列的内容。

写入示例:

注意:03和07版excel的对象的区别和文件后缀。

读取示例:

   @Testpublic void read03() throws IOException {FileInputStream fileInputStream = new FileInputStream(path + "apache-03.xls");Workbook workbook = new HSSFWorkbook(fileInputStream);Sheet sheet = workbook.getSheetAt(0);Row row = sheet.getRow(2);Cell cell = row.getCell(1);System.out.println(cell.getCellType());double value = cell.getNumericCellValue();System.out.println(value);if(fileInputStream != null) fileInputStream.close();}@Testpublic void read07() throws IOException {FileInputStream fileInputStream = new FileInputStream(path + "apache-07.xlsx");Workbook workbook = new XSSFWorkbook(fileInputStream);Sheet sheet = workbook.getSheetAt(0);Row row = sheet.getRow(1);Cell cell = row.getCell(1);System.out.println(cell.getCellType());String value = cell.getStringCellValue();System.out.println(value);if(fileInputStream != null) fileInputStream.close();}

注意:

读取时注意单元格的类型,按照类型进行使用不同的方法进行读取,使用的方法和类型不一致时会报错 

读取类型示例:

@Testpublic void cellTypeTest() throws IOException {FileInputStream fileInputStream = new FileInputStream(path + "apache-03.xls");Workbook workbook = new HSSFWorkbook(fileInputStream);Sheet sheet = workbook.getSheetAt(0);Row row = sheet.getRow(1);if (row != null) {int cellNum = row.getPhysicalNumberOfCells();for (int i = 0; i < cellNum; i++) {Cell cell = row.getCell(i);if (cell != null) {System.out.println(cell.getStringCellValue());}}}int rowNum = sheet.getPhysicalNumberOfRows();for (int i = 1; i < rowNum; i++) {Row row1 = sheet.getRow(i);if (row1 != null) {int cells = row1.getPhysicalNumberOfCells();for (int j = 0; j < cells; j++) {Cell cell = row1.getCell(j);if (cell == null) {continue;}String value = "";CellType cellType = cell.getCellType();switch (cellType) {case STRING:System.out.println("String类型");value = cell.getStringCellValue();break;case BLANK:System.out.println("类型为空");break;case BOOLEAN:System.out.println("布尔类型");value = String.valueOf(cell.getBooleanCellValue());break;case NUMERIC:if (DateUtil.isCellDateFormatted(cell)) {System.out.println("日期类型时");Date dateCellValue = cell.getDateCellValue();value = new DateTime(dateCellValue).toString("yyyy-MM-dd");} else {System.out.println("数值格式时");// 有时候数值过长会出现问题value = String.valueOf(cell.getNumericCellValue());}break;case ERROR:System.out.println("数据类型错误");break;default:}}}}if(fileInputStream != null) fileInputStream.close();}

 读取公式示例:

 @Testpublic void testFormula() throws IOException {String path = "C:\\Users\\DELL\\Desktop\\新建 XLS 工作表.xls";FileInputStream fileInputStream = new FileInputStream(path);Workbook workbook = new HSSFWorkbook(fileInputStream);Sheet sheet = workbook.getSheetAt(0);Row row = sheet.getRow(3);Cell cell = row.getCell(0);CellType cellType = cell.getCellType();switch (cellType) {case FORMULA:String cellFormula = cell.getCellFormula();System.out.println("公式:" + cellFormula);FormulaEvaluator formulaEvaluator = new HSSFFormulaEvaluator((HSSFWorkbook) workbook);CellValue evaluate = formulaEvaluator.evaluate(cell);String value = evaluate.formatAsString();System.out.println(value);}}

 根据类型判断是公式时,获取工作簿的计算对象,调用计算方法获取计算结果。

(3)写入说明

使用HSSF:会写入缓存,不操作磁盘,最后一次性写入磁盘,速度快。

使用XSSF:写数据时非常慢,非常耗内存,数据量大时,会发生内存溢出问题。

使用SXSSF:可以写非常大的数据量,如百万级别的数据,写数据速度快,占用更少的内存。

写入过程中会生成临时文件,需要手动清理临时文件,默认是100条数据保存在内存中,超过100条,则最前面的100条数据会写入到临时文件中,如果想自定义存放在内存中数据的数量,创建时可以传入件数的参数。如果写入的excel结构比较复杂,例如合并单元格,注释等,广泛使用也可能消耗大量内存。内存问题可以使用Jprofile进行监控。

写入示例:

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;
import org.junit.Test;import java.io.FileOutputStream;
import java.io.IOException;/*** @Author linab* @Date 2023/10/29 20:26* @Version 1.0*/
public class ApacheTest {private String path = "E:\\learn\\excel\\excel-poi\\src\\main\\resources\\";@Testpublic void test03() throws IOException {Workbook workbook = new HSSFWorkbook();Sheet sheet = workbook.createSheet("03");Row row = sheet.createRow(0);Cell cell = row.createCell(0);cell.setCellValue("姓名");Cell cell1 = row.createCell(1);cell1.setCellValue("小李");Row row1 = sheet.createRow(1);Cell cell2 = row1.createCell(0);cell2.setCellValue("日期");Cell cell3 = row1.createCell(1);String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");cell3.setCellValue(time);FileOutputStream fileOutputStream = new FileOutputStream(path + "apache-03.xls");workbook.write(fileOutputStream);if(fileOutputStream != null) fileOutputStream.close();if(workbook != null) workbook.close();System.out.println("文件生成成功");}@Testpublic void test07() throws IOException {Workbook workbook = new XSSFWorkbook();Sheet sheet = workbook.createSheet("07");Row row = sheet.createRow(0);Cell cell = row.createCell(0);cell.setCellValue("姓名");Cell cell1 = row.createCell(1);cell1.setCellValue("小李");Row row1 = sheet.createRow(1);Cell cell2 = row1.createCell(0);cell2.setCellValue("日期");Cell cell3 = row1.createCell(1);String time = new DateTime().toString("yyyy-MM-dd HH:mm:ss");cell3.setCellValue(time);FileOutputStream fileOutputStream = new FileOutputStream(path + "apache-07.xlsx");workbook.write(fileOutputStream);if(fileOutputStream != null) fileOutputStream.close();if(workbook != null) workbook.close();System.out.println("文件生成成功");}
}

写入速度示例:

    @Testpublic void testBigHssf() throws IOException {Instant start = Instant.now();HSSFWorkbook workbook = new HSSFWorkbook();HSSFSheet sheet = workbook.createSheet("hssf");for (int i = 0; i < 65536; i++) {HSSFRow row = sheet.createRow(i);for (int j = 0; j < 10; j++) {HSSFCell cell = row.createCell(j);cell.setCellValue(j);}}FileOutputStream fileOutputStream = new FileOutputStream(path + "bigHssf.xls");workbook.write(fileOutputStream);if (fileOutputStream != null) fileOutputStream.close();if (workbook != null) workbook.close();Instant end = Instant.now();System.out.println(Duration.between(start, end).toMillis());}@Testpublic void testBigXssf() throws IOException {Instant start = Instant.now();Workbook workbook = new XSSFWorkbook();Sheet sheet = workbook.createSheet("hssf");for (int i = 0; i < 65536; i++) {Row row = sheet.createRow(i);for (int j = 0; j < 10; j++) {Cell cell = row.createCell(j);cell.setCellValue(j);}}FileOutputStream fileOutputStream = new FileOutputStream(path + "bigXssf.xlsx");workbook.write(fileOutputStream);if (fileOutputStream != null) fileOutputStream.close();if (workbook != null) workbook.close();Instant end = Instant.now();System.out.println(Duration.between(start, end).toMillis());}@Testpublic void testBigSxssf() throws IOException {Instant start = Instant.now();Workbook workbook = new SXSSFWorkbook();Sheet sheet = workbook.createSheet("hssf");for (int i = 0; i < 65536; i++) {Row row = sheet.createRow(i);for (int j = 0; j < 10; j++) {Cell cell = row.createCell(j);cell.setCellValue(j);}}FileOutputStream fileOutputStream = new FileOutputStream(path + "bigsXssf.xlsx");workbook.write(fileOutputStream);if (fileOutputStream != null) fileOutputStream.close();((SXSSFWorkbook) workbook).dispose();if (workbook != null) workbook.close();Instant end = Instant.now();System.out.println(Duration.between(start, end).toMillis());}

4.easyExcel

        Java解析、生成Excel比较有名的架有Apache poi、jxl,但他们都存在一个严重的问题就是非常的耗内存,poi有一套SAX模式的API可以一定程度的解决一些内存溢出的问题,但POI还是有一些缺陷,比如07版Excel解压缩以及解压后存储都是在内存中完成的,内存消耗依然很大,easyexcel重写了poi对07版Excel的解析,能够将原本一个3M的excel用POl sax依然需要100M左右内存降低到几M,并且再大的excel不会出现内存溢出,03版依赖POI的sax模式,在上层做了模型转换的封装,让使用者更加简单方便。

EasyExcel是阿里巴巴开源的一个excel处理框架,以使用简单,节省内存著称。

节省内存的主要原因是在解析excel时没有将文件数据一次性全部加载到内存中,而是从磁盘上一行行读取数据,逐个解析。

读写excel只需要一行代码就可以完成。

详细的参照官网:读Excel | Easy Excel

5.总结

使用easyExcel让操作excel更加方便,如果数据量过大,不想一直存放在内存中,可以读取一些,处理一些,获取登录到数据库中。但有时要确保原子性,还要要放在内存中,所有数据校验都通过之后再存储到数据库中。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/127128.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

用pd.DataFrame.to_sql方法插入万行数据耗时21秒

to_sql是Pandas中用于将DataFrame数据写入数据库的方法&#xff0c;可以将DataFrame转换为SQL语句&#xff0c;方便我们将数据存入数据库中&#xff0c;以便进行后续的操作。 to_sql方法中包含多个参数&#xff0c;比较常用的参数有name&#xff08;表名&#xff09;、con&…

【算法挑战】用栈实现队列(含解析、源码)

232.用栈实现队列 https://leetcode-cn.com/problems/implement-queue-using-stacks/ 232.用栈实现队列 题目描述方法 1 思路复杂度代码 方法 2 思路复杂度代码(JavaScript/C) 题目描述 使用栈实现队列的下列操作&#xff1a;push(x) -- 将一个元素放入队列的尾部。 pop(…

系统架构设计师-第15章-面向服务架构设计理论与实践-软考学习笔记

面向服务的体系结构&#xff08;Service-Oriented Architecture, SOA) 面向服务的体系结构&#xff08;Service-Oriented Architecture, SOA&#xff09;是一种软件架构模式&#xff0c;它将应用程序的不同功能组织为一组可重用的、松耦合的、自治的服务&#xff0c;这些服务通…

在前端实现小铃铛上展示消息

点击铃铛显示如下消息框&#xff1a; 如果点击消息&#xff0c;可以实现消息从列表中移除,并从铃铛总数上进行扣减对应的已读消息数。 关于以上功能的实现方式&#xff1a; <!-- 铃铛位置 --><i class"el-icon-bell" click"showPopover true"&…

ubuntu启动报错error: proc_thermal_add, will cont

如题&#xff0c;ubuntu启动报错error: proc_thermal_add, will cont 截图如下&#xff1a; 困扰了我很久&#xff0c;差点就打算重装系统&#xff0c;准备放弃了&#xff0c;但是感谢国外的老哥&#xff0c;写了一篇非常详细的解决方案&#xff0c;我搬过来。 解决方案&#…

03-对象

对象 对象1.对象的创建字面量模式构造函数模式 2.对象的访问3.新增删除对象中的属性4.Object显示类型转换(强制类型转换)ECMAScript中可用的3种强制类型转换如下&#xff1a;Boolean(value)String(value)Number(value)Object类型到Boolean类型Object类型转String类型转换规则&a…

Redis通过复制rdb文件方式同步线上数据到本地以及提示:Can‘t handle RDB format version 9解决

场景 Redis的持久化机制-RDB方式和AOF方式&#xff1a; Redis的持久化机制-RDB方式和AOF方式_rdb 和ao-CSDN博客 Redis持久化机制导致服务自启动后恢复数据过长无法使用以及如何关闭&#xff1a; Redis持久化机制导致服务自启动后恢复数据过长无法使用以及如何关闭_霸道流氓…

mysql数据表设计

命名 mysql表名的命名规范为表名可以用 t_ 、tb_的前缀&#xff0c;或者是业务模块前缀。比如t_order。 有些项目也会使用 tt_、tm_、 ts_ 等前缀&#xff0c;根据项目的习惯命名就好了。 主键&#xff1a; AUTO_INCREMENT 表示自增&#xff0c;UNSIGNED 表示无符号&#xf…

【算法专题】双指针—盛最多水的容器

一、题目解析 分析这个题目不难得出一个容积公式 二、算法原理 解法一&#xff1a;暴力枚举&#xff08;超时&#xff09; 套用上述的容积公式&#xff0c;使用两个for循环来枚举出所有可能的情况&#xff0c;再挑出最大值即可&#xff0c;但是这种写法会超时&#xff0c;导致…

数据结构-初识泛型

写在前&#xff1a; 这一篇博客主要来初步的记录以下泛型的相关内容&#xff0c;内容比较琐碎&#xff0c;就不进行目录的整合&#xff0c;后续可能会对泛型这里进行系统性的梳理&#xff0c;此篇博客主要是对泛型有一个简单的认识与理解&#xff0c;需要知晓的内容。 当我调用…

2. 网络之网络编程

网络编程 文章目录 网络编程1. UDP1.1 DatagramSocket1.1.1 DatagramSocket 构造方法1.1.2 DatagramSocket 方法&#xff1a; 1.2 DatagramPacket1.2.1 DatagramPacket构造方法1.2.2 DaragramPacket方法1.2.3InetSocketAddress API 1.3 UDP回显服务器1.3.1 框架结构1.3.2 读取请…

将图像的锯齿状边缘变得平滑的方法

项目背景 使用PaddleSeg 192x192 模型分割出来的目标有锯齿状边缘&#xff0c;想通过传统算法将这种锯齿状边缘的变得平滑&#xff0c;虽然试了很过方法&#xff0c;但是效果还是不太理想 常用的集中方法 当使用分割算法&#xff08;如分水岭分割、阈值分割等&#xff09;分…

Docker:命令

Docker&#xff1a;命令 1. 创建MySQL的命令解读2. 基础命令3. 案例 查看DockerHub&#xff0c;拉取Nginx镜像&#xff0c;创建并运行Nginx容器4. 命令别名附录 1. 创建MySQL的命令解读 docker run :创建并运行一个容器&#xff0c;-d 是让容器在后台运行--name:给容器起一个名…

使用脚本整合指定文件/文件夹,执行定制化 ESLint 命令

背景 最近面对一个庞大的项目&#xff0c;但是只需要修改某个模块&#xff0c;每次都手搓命令太麻烦了&#xff0c;于是就想着能不能写个脚本来辅助处理这些事情。 解决方案 定制化一键 ESLint&#xff0c;执行文件下载地址&#xff1a; https://github.com/mazeyqian/go-g…

Python 自动化(十六)静态文件处理

准备工作 将不同day下的代码分目录管理&#xff0c;方便后续复习查阅 (testenv) [rootlocalhost projects]# ls day01 day02 (testenv) [rootlocalhost projects]# mkdir day03 (testenv) [rootlocalhost projects]# cd day03 (testenv) [rootlocalhost day03]# django-admi…

基于nodejs+vue啄木鸟便民维修网站设计与实现

目 录 摘 要 I ABSTRACT II 目 录 II 第1章 绪论 1 1.1背景及意义 1 1.2 国内外研究概况 1 1.3 研究的内容 1 第2章 相关技术 3 2.1 nodejs简介 4 2.2 express框架介绍 6 2.4 MySQL数据库 4 第3章 系统分析 5 3.1 需求分析 5 3.2 系统可行性分析 5 3.2.1技术可行性&#xff1a;…

element-plus走马灯不显示

问题描述 依赖正确&#xff0c;代码用法正确&#xff0c;但是element-plu走马灯就是不显示&#xff01;&#xff01; <div class"content"><el-carousel height"150px" width"200px"><el-carousel-item v-for"item in 4&qu…

Android 默认关闭自动旋转屏幕功能

Android 默认关闭自动旋转屏幕功能 接到客户邮件想要默认关闭设备的自动旋转屏幕功能&#xff0c;具体修改参照如下&#xff1a; /vendor/mediatek/proprietary/packages/apps/SettingsProvider/res/values/defaults.xml - <bool name"def_accelerometer_rotati…

代码随想录图论并查集 | 第六天 1971. 寻找图中是否存在路径 684.冗余连接

代码随想录图论并查集 | 第六天 1971. 寻找图中是否存在路径 684.冗余连接 一、1971. 寻找图中是否存在路径 题目链接&#xff1a;https://leetcode.cn/problems/find-if-path-exists-in-graph/ 思路&#xff1a;典型并查集模板题。 class Solution {int[] father null;pub…

1、Flink基础概念

1、基础知识 &#xff08;1&#xff09;、数据流上的有状态计算 &#xff08;2&#xff09;、框架和分布式处理引擎&#xff0c;用于对无界和有界数据流进行有状态计算。 &#xff08;3&#xff09;、事件驱动型应用&#xff0c;有数据流就进行处理&#xff0c;无数据流就不…