阿里的Easyexcel读取Excel文件(最新版本)
本篇文章主要介绍一下使用阿里开源的Easyexcel工具处理读取excel文件,因为之前自己想在网上找一下这个简单的立即上手的博客,发现很多文章的教程都针对比较旧的版本的Easyexcel,没有使用新版本的方法,导致很多方法都标志过期了或者运行时报错,所以本篇博客主要是使用最新版的Easyexcel去读取excel文件,顺便说一下目前新版本的特性。
优化
- 目前读取excel文件不再需要指定
ExcelTypeEnum,即excel的版本,会自动处理 - 之前创建
ExcelReader都是自己new,现在是通过EasyExcelFactory创建,更加简单和具备通用性。 - 之前每解析一行的回调的
invoke()方法,通用对象Object是list集合,目前是HashMap集合。

简单使用读取Excel,返回List集合
- 通过maven引入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.0.5</version>
</dependency>
- 新建通用监听类
StringExcelListener
/**
* StringList 解析监听器
*
* @author zhangcanlong
* @since 2019-10-21
*/
private static class StringExcelListener extends AnalysisEventListener {
/**
* 自定义用于暂时存储data
* 可以通过实例获取该值
*/
private List<List<String>> datas = new ArrayList<>();
/**
* 每解析一行都会回调invoke()方法
*
* @param object 读取后的数据对象
* @param context 内容
*/
@Override
public void invoke(Object object, AnalysisContext context) {
@SuppressWarnings("unchecked") Map<String, String> stringMap = (HashMap<String, String>) object;
//数据存储到list,供批量处理,或后续自己业务逻辑处理。
datas.add(new ArrayList<>(stringMap.values()));
//根据自己业务做处理
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
//解析结束销毁不用的资源
//注意不要调用datas.clear(),否则getDatas为null
}
/**
* 返回数据
*
* @return 返回读取的数据集合
**/
public List<List<String>> getDatas() {
return datas;
}
/**
* 设置读取的数据集合
*
* @param datas 设置读取的数据集合
**/
public void setDatas(List<List<String>> datas) {
this.datas = datas;
}
}
- 创建
ExcelReader读取,并从监听类中获取读取的数据
/**
* 根据excel输入流,读取excel文件
*
* @param inputStream exece表格的输入流
* @return 返回双重list的集合
**/
public List<List<String>> writeWithoutHead(InputStream inputStream) {
StringExcelListener listener = new StringExcelListener();
ExcelReader excelReader = EasyExcelFactory.read(inputStream, null, listener).headRowNumber(0).build();
excelReader.read();
List<List<String>> datas = listener.getDatas();
excelReader.finish();
return datas;
}
完整的Excel简单读取类和测试
测试类:
import com.hiido.services.common.ExcelOptionsService;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
/**
* excel操作的测试类
*
* @author zhangcanlong
* @since 2019/10/20 21:12
**/
@RunWith(SpringRunner.class)
@SpringBootTest
public class ExcelOptionsServiceTest {
@Autowired
private ExcelOptionsService excelOptionsService;
/**
* 测试读取excel
**/
@Test
public void testReadExcel() {
// 这里的excel文件可以 为xls或xlsx结尾
File file = new File("C:\\Users\\Administrator\\Desktop\\测试.xls");
List<List<String>> result = new ArrayList<>();
try {
result = excelOptionsService.writeWithoutHead(new FileInputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Assert.assertNotNull(result);
System.out.println("读取结果:" + result);
}
}
读取类
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import org.springframework.stereotype.Service;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* excel文件的操作service
*
* @author zhangcanlong
* @since 2019/10/20 21:01
**/
@Service
public class ExcelOptionsService {
/**
* 根据excel输入流,读取excel文件
*
* @param inputStream exece表格的输入流
* @return 返回双重list的集合
**/
public List<List<String>> writeWithoutHead(InputStream inputStream) {
StringExcelListener listener = new StringExcelListener();
ExcelReader excelReader = EasyExcelFactory.read(inputStream, null, listener).headRowNumber(0).build();
excelReader.read();
List<List<String>> datas = listener.getDatas();
excelReader.finish();
return datas;
}
/**
* StringList 解析监听器
*
* @author zhangcanlong
* @since 2019-10-21
*/
private static class StringExcelListener extends AnalysisEventListener {
/**
* 自定义用于暂时存储data
* 可以通过实例获取该值
*/
private List<List<String>> datas = new ArrayList<>();
/**
* 每解析一行都会回调invoke()方法
*
* @param object 读取后的数据对象
* @param context 内容
*/
@Override
public void invoke(Object object, AnalysisContext context) {
@SuppressWarnings("unchecked") Map<String, String> stringMap = (HashMap<String, String>) object;
// 这里可以获取excel的基本信息,包含excel的总行数
System.out.println("不一定十分准确的总行数:"+context.getTotalCount());
//数据存储到list,供批量处理,或后续自己业务逻辑处理。
datas.add(new ArrayList<>(stringMap.values()));
//根据自己业务做处理
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
//解析结束销毁不用的资源
//注意不要调用datas.clear(),否则getDatas为null
}
/**
* 返回数据
*
* @return 返回读取的数据集合
**/
public List<List<String>> getDatas() {
return datas;
}
/**
* 设置读取的数据集合
*
* @param datas 设置读取的数据集合
**/
public void setDatas(List<List<String>> datas) {
this.datas = datas;
}
}
}
参考:
- https://blog.csdn.net/alinyua/article/details/82859577
- https://github.com/alibaba/easyexcel/blob/master/quickstart.md

阿里的Easyexcel读取Excel文件(最新版本)的更多相关文章
- 读取Excel文件的版本
读取xls文件和xlsx文件创建的版本号. 虽然xlsx声明的是向前兼容,但是不知道OleDb是不是也是这样,没有办法所以要读取文件版本,限定只能读取Excel2007保存的文件. using ICS ...
- C#读取excel文件的内容(使用DataSet)
C#读取Excel文件的内容,通过OLEDB来连接,关键是连接的路径,如:string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data S ...
- 基于.NET的程序读取Excel文件的解决方案
目录 0. 前言 1. 使用NPOI库读取Excel文件 2. 使用OleDbConnection 3. 相关参考 shanzm-2020年12月8日 23:48:11 0. 前言 以前基于 .NET ...
- ADO.NET 读取Excel文件,并作数据源
项目中需要用的功能,贴上代码了. 需要注意的地方:配置Web.config的时候要注意版本问题! //若是在Web.config中配置数据源,如下 <add key="ExcelCon ...
- PHPExcel读取Excel文件的实现代码
<?php require_once 'PHPExcel.php'; /**对excel里的日期进行格式转化*/ function GetData($val){ $jd = GregorianT ...
- .Net读取Excel文件时丢失数据的问题 (转载)
相信很多人都试过通过OleDB读取Excel文件,这种方法效率十分高,只是有一点会让人十分头痛,就是当一列中既有混合型数据,又有纯数据时,往往容易丢失数据. 百度过后,改连接字符串 “HDR=YES; ...
- C# conn.open() 外部表不是预期的格式( 读取EXCEL文件出错)
环境:win7+iis7+Office2007 在asp.net网站中导出Excel文件后,再把文件导入到数据库中. 读取Excel文件时,打开连接出错. 错误为:外部表不是预期的格式 解决:检查了一 ...
- (实用篇)PHPExcel读取Excel文件的实现代码
用PHPExcel读取Excel 2007 或者Excel2003文件,需要的朋友,可以参考下. 涉及知识点: php对excel文件进行循环读取 php对字符进行ascii编码转化,将字符转为十进 ...
- 读取Excel文件的两种方法
第一种方法:传统方法,采用OleDB读取EXCEL文件, 优点:写法简单,缺点:服务器必须安有此组件才能用,不推荐使用 private DataSet GetConnect_DataSet2(stri ...
随机推荐
- Educational Codeforces Round 89 (Rated for Div. 2) B. Shuffle (数学,区间)
题意:有长为\(n\)的排列,其中\(x\)位置上的数为\(1\),其余位置全为\(0\),询问\(m\)次,每次询问一个区间,在这个区间内可以交换任意两个位置上的数,问\(1\)最后出现在不同位置的 ...
- cdn jsdelivr + github releases 以wordpress sakura主题manifest为例
1 创建github repository 在本地创建文件,这里为文件夹 /manifest 在github创建库wordpresscdn,上传 /manifest到库中 2 github relea ...
- Dapr 交通控制示例
Dapr 已在塔架就位 将发射新一代微服务 牛年 dotnet云原生技术趋势 Dapr是如何简化微服务的开发和部署 前面几篇文章都是从大的方面给大家分享Dapr 能帮助我们解决什么问题,微软从开源到1 ...
- Gif2mp4 by Python
参考链接 目的: $ .gif \rightarrow .mp4 $ 解决: pip install MoviePy import moviepy.editor as mp clip = mp.Vid ...
- 最新 uni-app 免费教程
最新 uni-app 免费教程 uni-app 快速入门 steps 建议第一步,看完uni-app官网的首页介绍. 建议第二步,通过快速上手,亲身体验下uni-app. 建议第三步,看完<un ...
- SVG in Action
SVG in Action HTML5 semantic HTML5 Semantic Elements / HTML5 Semantic Tags figure object <figure& ...
- 前端 vs 后端
前端 vs 后端 前端与后端: 有什么区别? 前端和后端是计算机行业中最常用的两个术语. 在某种程度上,它们成了流行语. 它们决定了您作为软件开发人员所从事的工作类型,所使用的技术以及所获得的收入. ...
- how to read the 10th line of a text using shell script
how to read the 10th line of a text using shell script shell script / bash script question https://l ...
- Web 安全 & cookies & HttpOnly
Web 安全 & cookies & HttpOnly cookie HttpOnly 禁止 js 读取 cookie 的方法 HttpOnly 实现原理 document.cooki ...
- js & replaceAll & non-global RegExp bug
js & replaceAll https://caniuse.com/#search=replaceAll https://developer.mozilla.org/en-US/docs/ ...