it编程 > 编程语言 > C#

C#中ExcelDataReader的具体使用

11人参与 2025-04-24 C#

今天来给大家介绍一下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内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)
打赏 微信扫一扫 微信扫一扫

您想发表意见!!点此发布评论

推荐阅读

C#和Java互相调用的方法小结

04-24

C# 窗口过程消息处理 WndProc的方法详解

04-24

C# WinForms存储过程操作数据库的实例讲解

04-24

使用C#进行音频处理的完整指南(从播放到编辑)

04-24

C#中高效的多线程并行处理实现方式详解

04-24

C# 配置文件app.config 和 web.config详解

04-24

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论