ExcelDataReader

2025-12-11 0 133

ExcelDataReader

轻量级和快速库编写的C#读取Microsoft Excel文件(2.0-2021,365)。

请随时向叉子提交拉动请求,向开发分支提交。

如果您要报告一个问题,如果您可以提供示例Excel文件,那么这确实有用,因为这会使调试变得更加容易,并且没有它,我们可能无法解决任何问题。

连续整合

分支 建立状态
发展
掌握

支持的文件格式版本

文件类型 容器格式 文件格式 Excel版本
.xlsx ZIP,CFB+ZIP OpenXML 2007年和更新
.xlsb Zip,CFB OpenXML 2007年和更新
.xls CFB Biff8 97,2000,XP,2003年
98,2001,VX,2004(MAC)
.xls CFB Biff5 5.0,95
.xls Biff4 4.0
.xls Biff3 3.0
.xls Biff2 2.0,2.2
.csv CSV (全部)

找到二进制文件

建议通过VS软件包管理台Install-Package <package>使用Nuget或使用VS“管理Nuget软件包…”扩展名。

从ExcelDataReader版本3.0开始,该项目被分为多个软件包:

安装ExcelDataReader基本软件包以使用“低级”读取器接口。与Net462,NetStandard2.0和NetStandard2.1兼容。

安装ExcelDataReader .DataSet扩展程序包以使用AsDataSet()方法填充System.Data.DataSet 。这也将拉入基本包。与Net462,NetStandard2.0和NetStandard2.1兼容。

如何使用

 using ( var stream = File . Open ( filePath , FileMode . Open , FileAccess . Read ) )
{
    // Auto-detect format, supports:
    //  - Binary Excel files (2.0-2003 format; *.xls)
    //  - OpenXml Excel files (2007 format; *.xlsx, *.xlsb)
    using ( var reader = ExcelReaderFactory . CreateReader ( stream ) )
    {
        // Choose one of either 1 or 2:

        // 1. Use the reader methods
        do
        {
            while ( reader . Read ( ) )
            {
                // reader.GetDouble(0);
            }
        } while ( reader . NextResult ( ) ) ;

        // 2. Use the AsDataSet extension method
        var result = reader . AsDataSet ( ) ;

        // The result of each spreadsheet is in result.Tables
    }
}

读取.csv文件

使用ExcelReaderFactory.CreateCsvReader而不是CreateReader来解析具有逗号分隔值的纯文本。

另请参阅配置选项FallbackEncodingAutodetectSeparators

输入CSV总是一次完全解析,以设置FieldCount,RowCount,编码,分离器(如果CSV缺乏BOM而不是UTF8,则两次),然后在迭代行记录时再次解析。 throws System.Text.DecoderFallbackException如果输入无法用指定的编码解析。

读者将所有CSV字段值作为字符串返回,并且没有尝试将数据转换为数字或日期。该呼叫者负责解释CSV数据。

使用阅读器方法

AsDataSet()扩展方法是快速获取数据的方便助手,但并非总是可用或可取的。我ExcelDataReader扩展了System.Data.IDataReaderIDataRecord Interfaces,以较低级别导航和检索数据。最重要的读取器方法和属性:

方法 财产
Read() 从当前表读取一行。
NextResult() 将光标推向下一张纸。
ResultsCount 返回当前工作簿中的床单。
Name 返回当前表的名称。
CodeName 返回当前表的VBA代码名称标识符。
FieldCount 返回当前表中的列数。
RowCount 返回当前表中的行数。这包括终端空行,否则这些行被Asdataset()排除在外。与AnalyzeInitialCsvRows一起使用时,在CSV文件上抛出InvalidOperationException
HeaderFooter 返回一个具有有关标头和页脚的信息的对象,或者如果没有的话,则null
MergeCells 返回当前表中合并的单元格范围。
RowHeight 返回当前行的视觉高度。如果隐藏行,则可能是0。
GetColumnWidth() 返回字符单元中的列的宽度。如果列隐藏,则可能是0。
GetFieldType() 返回当前行中值的类型。如果没有值,则始终是Excel支持的类型之一: doubleintboolDateTimeTimeSpanstringnull
IsDBNull() 检查当前行中的值是否为null。
GetValue() 从当前行返回一个值作为object ,如果没有值,则null
GetDouble()
GetInt32()
GetBoolean()
GetDateTime()
GetString()
从当前行铸造的值返回其各自类型。
GetNumberFormatString() 返回一个包含格式代码的字符串,用于当前行中的值,如果没有值,则返回null 。另请参见下面的格式部分。
GetNumberFormatIndex() 返回当前行中值的数字格式索引。索引值低于164,请参阅内置数字格式,否则表示自定义数字格式。
GetCellStyle() 返回当前行中包含单元格的样式信息的对象:凹痕,水平对齐,隐藏,锁定。
键入的Get*()方法 除非类型完全匹配,否则投掷InvalidCastException

CreateAder()配置选项

ExcelReaderFactory.CreateReader()CreateBinaryReader()CreateOpenXmlReader()CreateCsvReader()方法接受可选的配置对象,以修改阅读器的行为:

ExcelDataReader object is disposed. Default: false
LeaveOpen = false,

// Gets or sets a value indicating the number of rows to analyze for
// encoding, separator and field count in a CSV. When set, this option
// causes the I ExcelDataReader .RowCount property to throw an exception.
// Default: 0 – analyzes the entire file (CSV only, has no effect on other
// formats)
AnalyzeInitialCsvRows = 0,
});\”>

 var reader = ExcelReaderFactory . CreateReader ( stream , new ExcelReaderConfiguration ( )
{
    // Gets or sets the encoding to use when the input XLS lacks a CodePage
    // record, or when the input CSV lacks a BOM and does not parse as UTF8. 
    // Default: cp1252 (XLS BIFF2-5 and CSV only)
    FallbackEncoding = Encoding . GetEncoding ( 1252 ) ,

    // Gets or sets the password used to open password protected workbooks.
    Password = \"password\" ,

    // Gets or sets an array of CSV separator candidates. The reader 
    // autodetects which best fits the input data. Default: , ; TAB | # 
    // (CSV only)
    AutodetectSeparators = new char [ ] { \',\' , \';\' , \' \\t \' , \'|\' , \'#\' } ,

    // Gets or sets a value indicating whether to trim white space values for CSV (Default \'true\').
    // (CSV only)
    TrimWhiteSpace = true ,

    // Gets or sets a value indicating whether to leave the stream open after
    // the I ExcelDataReader object is disposed. Default: false
    LeaveOpen = false ,

    // Gets or sets a value indicating the number of rows to analyze for
    // encoding, separator and field count in a CSV. When set, this option
    // causes the I ExcelDataReader .RowCount property to throw an exception.
    // Default: 0 - analyzes the entire file (CSV only, has no effect on other
    // formats)
    AnalyzeInitialCsvRows = 0 ,
} ) ;

Asdataset()配置选项

AsDataSet()方法接受可选的配置对象,以修改数据集转换的行为:

 var result = reader . AsDataSet ( new ExcelDataSetConfiguration ( )
{
    // Gets or sets a value indicating whether to set the DataColumn.DataType 
    // property in a second pass.
    UseColumnDataType = true ,

    // Gets or sets a callback to determine whether to include the current sheet
    // in the DataSet. Called once per sheet before ConfigureDataTable.
    FilterSheet = ( tableReader , sheetIndex ) => true ,

    // Gets or sets a callback to obtain configuration options for a DataTable. 
    ConfigureDataTable = ( tableReader ) => new ExcelDataTableConfiguration ( )
    {
        // Gets or sets a value indicating the prefix of generated column names.
        EmptyColumnNamePrefix = \"Column\" ,

        // Gets or sets a value indicating whether to use a row from the 
        // data as column names.
        UseHeaderRow = false ,

        // Gets or sets a callback to determine which row is the header row. 
        // Only called when UseHeaderRow = true.
        ReadHeaderRow = ( rowReader ) => {
            // F.ex skip the first row and use the 2nd row as column headers:
            rowReader . Read ( ) ;
        } ,

        // Gets or sets a callback to determine whether to include the 
        // current row in the DataTable.
        FilterRow = ( rowReader ) => {
            return true ;
        } ,

        // Gets or sets a callback to determine whether to include the specific
        // column in the DataTable. Called once per column after reading the 
        // headers.
        FilterColumn = ( rowReader , columnIndex ) => {
            return true ;
        }
    }
} ) ;

设置AsDataSet()配置,使用FilterRow回调在加载时实现“进度指示器”,例如:

 var result = reader . AsDataSet ( new ExcelDataSetConfiguration ( )
{
    ConfigureDataTable = ( tableReader ) => new ExcelDataTableConfiguration ( )
    {
        FilterRow = ( rowReader ) => {
            int progress = ( int ) Math . Ceiling ( ( decimal ) rowReader . Depth / ( decimal ) rowReader . RowCount * ( decimal ) 100 ) ;
            // progress is in the range 0..100
            return true ;
        }
    }
} ) ; 

格式化

ExcelDataReader不直接支持格式化。用户可以通过I ExcelDataReader .GetNumberFormatString(i)重新删除单元格的数字格式字符串,并使用第三方excelnumberformat库进行格式化。

使用ExcelDataReader和excelnumberformat的示例辅助方法,以格式化一个值:

ExcelDataReader reader, int columnIndex, CultureInfo culture)
{
var value = reader.GetValue(columnIndex);
var formatString = reader.GetNumberFormatString(columnIndex);
if (formatString != null)
{
var format = new NumberFormat(formatString);
return format.Format(value, culture);
}
return Convert.ToString(value, culture);
}\”>

 string GetFormattedValue ( I ExcelDataReader reader , int columnIndex , CultureInfo culture )
{
    var value = reader . GetValue ( columnIndex ) ;
    var formatString = reader . GetNumberFormatString ( columnIndex ) ;
    if ( formatString != null )
    {
        var format = new NumberFormat ( formatString ) ;
        return format . Format ( value , culture ) ;
    }
    return Convert . ToString ( value , culture ) ;
}

参见:

  • https://gi*th*ub*.com/andersnm/excelnumberformat
  • https://www.*nug**et.org/packages/excelnumberformat

从ExcelDataReader 2.x升级时要注意

ExcelDataReader 3发生了一些破裂的变化,较旧的代码可能会产生类似的错误消息:

ExcelDataReader\’ does not contain a definition for \’AsDataSet\’…
\’I ExcelDataReader \’ does not contain a definition for \’IsFirstRowAsColumnNames\’…\”>

 \'I ExcelDataReader \' does not contain a definition for \'AsDataSet\'...
\'I ExcelDataReader \' does not contain a definition for \'IsFirstRowAsColumnNames\'...

修复:

  1. 确保将代码中的任何Excel名称空间引用重命名为新名称空间ExcelDataReader

  2. 确保该项目对ExcelDataReader .DataSet软件包的参考来使用AsDataSet()

  3. 使用IsFirstRowAsColumnNames删除代码行,然后将呼叫更改为Asdataset()以下内容:

 var result = reader . AsDataSet ( new ExcelDataSetConfiguration ( )
{
    ConfigureDataTable = ( _ ) => new ExcelDataTableConfiguration ( )
    {
        UseHeaderRow = true
    }
} ) ; 

在.NET核心上的重要说明

默认情况下, ExcelDataReader抛出了一个notsupportedException“没有用于编码1252的数据。”在.NET Core和.NET 5.0或更高版本上。

要修复,请将依赖项添加到package System.Text.Encoding.CodePages ,然后在应用程序初始化期间添加代码以注册代码页提供商(instup.cs中的F.EX):

 System . Text . Encoding . RegisterProvider ( System . Text . CodePagesEncodingProvider . Instance ) ;

这是在二进制BIFF2-5 excel文档中解析字符串所必需的,该文档编码了DOS-era代码页面。这些编码默认情况下在完整的.NET框架中注册,但在.NET Core和.NET 5.0或更高版本上不注册。

下载源码

通过命令行克隆项目:

git clone https://github.com/ExcelDataReader/ExcelDataReader.git

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

申明:本文由第三方发布,内容仅代表作者观点,与本网站无关。对本文以及其中全部或者部分内容的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。本网发布或转载文章出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,也不代表本网对其真实性负责。

左子网 编程相关 ExcelDataReader https://www.zuozi.net/34101.html

centraldogma
上一篇: centraldogma
libimobiledevice
下一篇: libimobiledevice
常见问题
  • 1、自动:拍下后,点击(下载)链接即可下载;2、手动:拍下后,联系卖家发放即可或者联系官方找开发者发货。
查看详情
  • 1、源码默认交易周期:手动发货商品为1-3天,并且用户付款金额将会进入平台担保直到交易完成或者3-7天即可发放,如遇纠纷无限期延长收款金额直至纠纷解决或者退款!;
查看详情
  • 1、描述:源码描述(含标题)与实际源码不一致的(例:货不对板); 2、演示:有演示站时,与实际源码小于95%一致的(但描述中有”不保证完全一样、有变化的可能性”类似显著声明的除外); 3、发货:不发货可无理由退款; 4、安装:免费提供安装服务的源码但卖家不履行的; 5、收费:价格虚标,额外收取其他费用的(但描述中有显著声明或双方交易前有商定的除外); 6、其他:如质量方面的硬性常规问题BUG等。 注:经核实符合上述任一,均支持退款,但卖家予以积极解决问题则除外。
查看详情
  • 1、左子会对双方交易的过程及交易商品的快照进行永久存档,以确保交易的真实、有效、安全! 2、左子无法对如“永久包更新”、“永久技术支持”等类似交易之后的商家承诺做担保,请买家自行鉴别; 3、在源码同时有网站演示与图片演示,且站演与图演不一致时,默认按图演作为纠纷评判依据(特别声明或有商定除外); 4、在没有”无任何正当退款依据”的前提下,商品写有”一旦售出,概不支持退款”等类似的声明,视为无效声明; 5、在未拍下前,双方在QQ上所商定的交易内容,亦可成为纠纷评判依据(商定与描述冲突时,商定为准); 6、因聊天记录可作为纠纷评判依据,故双方联系时,只与对方在左子上所留的QQ、手机号沟通,以防对方不承认自我承诺。 7、虽然交易产生纠纷的几率很小,但一定要保留如聊天记录、手机短信等这样的重要信息,以防产生纠纷时便于左子介入快速处理。
查看详情

相关文章

猜你喜欢
发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务