今天来给大家介绍一下exceldatareader,exceldatareader是一个轻量级的可快速读取excel文件中数据的工具。话不多说直接开始。
exceldatareader简介
exceldatareader支持.xlsx、.xlsb、.xls、.csv格式文件的读取,版本基本在2007及以上版本,支持的格式和版本看下图。

我们首先需要再vs中添加exceldatareader程序集,在 工具 -> nuget包管理器 -> 管理解决方案的nuget程序包 中搜索exceldatareader,并安装到工程中。如下图:


exceldatareader和exceldatareader.dataset都可以调用exceldatareader库,官网介绍由于版本升级问题,如果需要使用asdataset()方法则需要安装exceldatareader.dataset。
exceldatareader使用
我们下来介绍一下exceldatareader对于数据的读取,有两种方式可以读取excel,iexceldatareader方法和dataset方法。
我们先来展示一下excel文件的示例,图1是sheet1,图2是sheet2


iexceldatareader
首先我们需要通过 file.openread 来获取 excel.xlsx 的文件流数据。通过 excelreaderfactory.createreader 接口来创建 iexceldatareader reader 对象,reader采取的是逐个工作表、逐行的方式读取数据,然后通过 reader.getstring(0) 获取数据。
reader默认为第一个工作表,第一行数据开始。调用 reader.nextresult() 接口 reader 会移入下一个工作表。调用 reader.read() 接口 reader 会读取一行数据。reader.getstring(0) 接口是通过索引来获取字符串数据,除此之外 reader 还有其他获取不同类型数据的接口,小伙伴可以自行查阅文档。
由于 iexceldatareader 采用的是逐页、逐行读取数据的方式,这种方式随便不够灵活便捷,但由于每次读取数据量少,所以在运行效率和性能上有比较大的优势。
using system.text;
using exceldatareader;
public class program
{
static void main(string[] args)
{
encoding.registerprovider(codepagesencodingprovider.instance);
string filepath = "excel.xlsx";
using (filestream stream = file.openread(filepath))
{
using (iexceldatareader reader = excelreaderfactory.createreader(stream))
{
// 1. use the reader methods
do
{
console.writeline($"sheet name:{reader.name}");
while (reader.read())
{
console.writeline($"{reader.getstring(0)} \t\t{reader.getstring(1)}");
}
console.writeline();
} while (reader.nextresult());
}
}
}
}asdataset
同样我们需要通过 file.openread 来获取 excel.xlsx 的文件流数据,然后通过 excelreaderfactory.createreader 接口来创建 iexceldatareader reader 对象,这与之前的操作一样。
之后我们通过 reader.asdataset() 接口来获取 dataset dataset 对象,dataset 可以采用索引的方式来获取数据。通过 dataset.tables[0] 可以获取到 datatable datatable 对象。datatable 就是excel的工作页,输出 datatable.tablename 可以获取到工作页名称。我们再通过 datatable.rows[0] 来获取 datarow datarow 对象,datarow 是当前工作页下每一行的数据,通过 datarow[0] 可以获取单元格中的数据。
除了索引获取数据以外,依然可以通过遍历的方式来获取数据,示例代码如下。
using system.data;
using system.text;
using exceldatareader;
public class program
{
static void main(string[] args)
{
encoding.registerprovider(codepagesencodingprovider.instance);
string filepath = "excel.xlsx";
using (filestream stream = file.openread(filepath))
{
using (iexceldatareader reader = excelreaderfactory.createreader(stream))
{
// 2. use the asdataset extension method
dataset dataset = reader.asdataset();
datatable datatable = dataset.tables[0];
console.writeline(datatable.tablename);
datarow datarow = datatable.rows[0];
console.writeline($"{datarow[0]} {datarow[1]}\n");
foreach (datatable table in dataset.tables)
{
console.writeline(table.tablename);
foreach (datarow row in table.rows)
{
console.writeline($"{row[0]} \t{row[1]}");
}
console.writeline();
}
}
}
}
}补充说明
需要补充说明一下的是 exceldatareader 在.net core 和 .net 5.0 或更高版本中会抛出encoding 1252的错误编码。解决办法就是需要为system.text.encoding.codepages添加一个依赖项,在代码中添加 encoding.registerprovider(codepagesencodingprovider.instance); 这段代码
官方文档链接
exceldatareader链接:https://github.com/exceldatareader/exceldatareader
到此这篇关于c#中exceldatareader的具体使用的文章就介绍到这了,更多相关c# exceldatareader内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论