Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Lightweight and fast library written in C# for reading Microsoft Excel files

License

NotificationsYou must be signed in to change notification settings

ExcelDataReader/ExcelDataReader

Repository files navigation

NuGet

Lightweight and fast library written in C# for reading Microsoft Excel files (2.0-2021, 365).

Please feel free to fork and submit pull requests to the develop branch.

If you are reporting an issue it is really useful if you can supply an example Excel file as this makes debugging much easier and without it we may not be able to resolve any problems.

Continuous integration

BranchBuild status
developBuild status
masterBuild status

Supported file formats and versions

File TypeContainer FormatFile FormatExcel Version(s)
.xlsxZIP, CFB+ZIPOpenXml2007 and newer
.xlsbZIP, CFBOpenXml2007 and newer
.xlsCFBBIFF897, 2000, XP, 2003
98, 2001, v.X, 2004 (Mac)
.xlsCFBBIFF55.0, 95
.xls-BIFF44.0
.xls-BIFF33.0
.xls-BIFF22.0, 2.2
.csv-CSV(All)

Finding the binaries

It is recommended to use NuGet through the VS Package Manager ConsoleInstall-Package <package> or using the VS "Manage NuGet Packages..." extension.

As of ExcelDataReader version 3.0, the project was split into multiple packages:

Install theExcelDataReader base package to use the "low level" reader interface. Compatible with net462, netstandard2.0 and netstandard2.1.

Install theExcelDataReader.DataSet extension package to use theAsDataSet() method to populate aSystem.Data.DataSet. This will also pull in the base package. Compatible with net462, netstandard2.0 and netstandard2.1.

How to use

using(varstream=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(varreader=ExcelReaderFactory.CreateReader(stream)){// Choose one of either 1 or 2:// 1. Use the reader methodsdo{while(reader.Read()){// reader.GetDouble(0);}}while(reader.NextResult());// 2. Use the AsDataSet extension methodvarresult=reader.AsDataSet();// The result of each spreadsheet is in result.Tables}}

Reading .CSV files

UseExcelReaderFactory.CreateCsvReader instead ofCreateReader to parse a stream of plain text with comma separated values.

See also the configuration optionsFallbackEncoding andAutodetectSeparators.

The input CSV is always parsed once completely to set FieldCount, RowCount, Encoding, Separator (or twice if the CSV lacks BOM and is not UTF8), and then parsed once again while iterating the row records. ThrowsSystem.Text.DecoderFallbackException if the input cannot be parsed with the specified encoding.

The reader returns all CSV field values as strings and makes no attempts to convert the data to numbers or dates. This caller is responsible for interpreting the CSV data.

Using the reader methods

TheAsDataSet() extension method is a convenient helper for quickly getting the data, but is not always available or desirable to use. IExcelDataReader extends theSystem.Data.IDataReader andIDataRecord interfaces to navigate and retrieve data at a lower level. The most important reader methods and properties:

MethodProperty
Read()reads a row from the current sheet.
NextResult()advances the cursor to the next sheet.
ResultsCountreturns the number of sheets in the current workbook.
Namereturns the name of the current sheet.
CodeNamereturns the VBA code name identifier of the current sheet.
FieldCountreturns the number of columns in the current sheet.
RowCountreturns the number of rows in the current sheet. This includes terminal empty rows which are otherwise excluded by AsDataSet(). ThrowsInvalidOperationException on CSV files when used withAnalyzeInitialCsvRows.
HeaderFooterreturns an object with information about the headers and footers, ornull if there are none.
MergeCellsreturns an array of merged cell ranges in the current sheet.
RowHeightreturns the visual height of the current row in points. May be 0 if the row is hidden.
GetColumnWidth()returns the width of a column in character units. May be 0 if the column is hidden.
GetFieldType()returns the type of a value in the current row. Always one of the types supported by Excel:double,int,bool,DateTime,TimeSpan,string, ornull if there is no value.
IsDBNull()checks if a value in the current row is null.
GetValue()returns a value from the current row as anobject, ornull if there is no value.
GetDouble()
GetInt32()
GetBoolean()
GetDateTime()
GetString()
return a value from the current row cast to their respective type.
GetNumberFormatString()returns a string containing the formatting codes for a value in the current row, ornull if there is no value. See also the Formatting section below.
GetNumberFormatIndex()returns the number format index for a value in the current row. Index values below 164 refer to built-in number formats, otherwise indicate a custom number format.
GetCellStyle()returns an object containing style information for a cell in the current row: indent, horizontal alignment, hidden, locked.
The typedGet*() methodsthrowInvalidCastException unless the types match exactly.

CreateReader() configuration options

TheExcelReaderFactory.CreateReader(),CreateBinaryReader(),CreateOpenXmlReader(),CreateCsvReader() methods accept an optional configuration object to modify the behavior of the reader:

varreader=ExcelReaderFactory.CreateReader(stream,newExcelReaderConfiguration(){// 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=newchar[]{',',';','\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 IExcelDataReader object is disposed. Default: falseLeaveOpen=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 IExcelDataReader.RowCount property to throw an exception.// Default: 0 - analyzes the entire file (CSV only, has no effect on other// formats)AnalyzeInitialCsvRows=0,});

AsDataSet() configuration options

TheAsDataSet() method accepts an optional configuration object to modify the behavior of the DataSet conversion:

varresult=reader.AsDataSet(newExcelDataSetConfiguration(){// 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)=>newExcelDataTableConfiguration(){// 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)=>{returntrue;},// 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)=>{returntrue;}}});

Setting upAsDataSet() configuration, use the FilterRow callback to implement a "progress indicator" while loading, e.g.:

varresult=reader.AsDataSet(newExcelDataSetConfiguration(){ConfigureDataTable=(tableReader)=>newExcelDataTableConfiguration(){FilterRow=(rowReader)=>{intprogress=(int)Math.Ceiling((decimal)rowReader.Depth/(decimal)rowReader.RowCount*(decimal)100);// progress is in the range 0..100returntrue;}}});

Formatting

ExcelDataReader does not support formatting directly. Users may retreive the number format string for a cell throughIExcelDataReader.GetNumberFormatString(i) and use the third party ExcelNumberFormat library for formatting purposes.

Example helper method using ExcelDataReader and ExcelNumberFormat to format a value:

stringGetFormattedValue(IExcelDataReaderreader,intcolumnIndex,CultureInfoculture){varvalue=reader.GetValue(columnIndex);varformatString=reader.GetNumberFormatString(columnIndex);if(formatString!=null){varformat=newNumberFormat(formatString);returnformat.Format(value,culture);}returnConvert.ToString(value,culture);}

See also:

Important note when upgrading from ExcelDataReader 2.x

ExcelDataReader 3 had some breaking changes, and older code may produce error messages similar to:

'IExcelDataReader' does not contain a definition for 'AsDataSet'...'IExcelDataReader' does not contain a definition for 'IsFirstRowAsColumnNames'...

To fix:

  1. Make sure to rename anyExcel namespace references in the code to the new namespaceExcelDataReader

  2. Make sure the project has a reference to theExcelDataReader.DataSet package to useAsDataSet()

  3. Remove the line of code withIsFirstRowAsColumnNames and change the call to AsDataSet() to something like this:

varresult=reader.AsDataSet(newExcelDataSetConfiguration(){ConfigureDataTable=(_)=>newExcelDataTableConfiguration(){UseHeaderRow=true}});

Important note on .NET Core

By default, ExcelDataReader throws a NotSupportedException "No data is available for encoding 1252." on .NET Core and .NET 5.0 or later.

To fix, add a dependency to the packageSystem.Text.Encoding.CodePages and then add code to register the code page provider during application initialization (f.ex in Startup.cs):

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

This is required to parse strings in binary BIFF2-5 Excel documents encoded with DOS-era code pages. These encodings are registered by default in the full .NET Framework, but not on .NET Core and .NET 5.0 or later.

About

Lightweight and fast library written in C# for reading Microsoft Excel files

Topics

Resources

License

Stars

Watchers

Forks

Languages


[8]ページ先頭

©2009-2025 Movatter.jp