ExcelDataReader
Lightweight and fast library written in C# for reading Microsoft Excel files (2.0-2007).
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
Branch | Build status |
---|---|
develop | |
master |
Supported file formats and versions
File Type | Container Format | File Format | Excel Version(s) |
---|---|---|---|
.xlsx | ZIP, CFB+ZIP | OpenXml | 2007 and newer |
.xlsb | ZIP, CFB | OpenXml | 2007 and newer |
.xls | CFB | BIFF8 | 97, 2000, XP, 2003 98, 2001, v.X, 2004 (Mac) |
.xls | CFB | BIFF5 | 5.0, 95 |
.xls | — | BIFF4 | 4.0 |
.xls | — | BIFF3 | 3.0 |
.xls | — | BIFF2 | 2.0, 2.2 |
.csv | — | CSV | (All) |
Finding the binaries
It is recommended to use NuGet through the VS Package Manager Console Install-Package <package>
or using the VS «Manage NuGet Packages…» extension.
As of ExcelDataReader version 3.0, the project was split into multiple packages:
Install the ExcelDataReader
base package to use the «low level» reader interface. Compatible with met462, netstandard2.0 and netstandard2.1.
Install the ExcelDataReader.DataSet
extension package to use the AsDataSet()
method to populate a System.Data.DataSet
. This will also pull in the base package. Compatible with net462, netstandard2.0 and netstandard2.1.
How to use
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 } }
Reading .CSV files
Use ExcelReaderFactory.CreateCsvReader
instead of CreateReader
to parse a stream of plain text with comma separated values.
See also the configuration options FallbackEncoding
and AutodetectSeparators
.
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. Throws System.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
The AsDataSet()
extension method is a convenient helper for quickly getting the data, but is not always available or desirable to use. IExcelDataReader extends the System.Data.IDataReader
and IDataRecord
interfaces to navigate and retrieve data at a lower level. The most important reader methods and properties:
Read()
reads a row from the current sheet.NextResult()
advances the cursor to the next sheet.ResultsCount
returns the number of sheets in the current workbook.Name
returns the name of the current sheet.CodeName
returns the VBA code name identifier of the current sheet.FieldCount
returns the number of columns in the current sheet.RowCount
returns 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
.HeaderFooter
returns an object with information about the headers and footers, ornull
if there are none.MergeCells
returns an array of merged cell ranges in the current sheet.RowHeight
returns 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 typed
Get*()
methods throwInvalidCastException
unless the types match exactly.
CreateReader() configuration options
The ExcelReaderFactory.CreateReader()
, CreateBinaryReader()
, CreateOpenXmlReader()
, CreateCsvReader()
methods accept an optional configuration object to modify the behavior of the reader:
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 leave the stream open after // the IExcelDataReader 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 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
The AsDataSet()
method accepts an optional configuration object to modify the behavior of the DataSet conversion:
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; } } });
Setting up AsDataSet()
configuration, use the FilterRow callback to implement a «progress indicator» while loading, e.g.:
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; } } });
Formatting
ExcelDataReader does not support formatting directly. Users may retreive the number format string for a cell through IExcelDataReader.GetNumberFormatString(i)
and use the third party ExcelNumberFormat library for formatting purposes.
Example helper method using ExcelDataReader and ExcelNumberFormat to format a value:
string GetFormattedValue(IExcelDataReader 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); }
See also:
- https://github.com/andersnm/ExcelNumberFormat
- https://www.nuget.org/packages/ExcelNumberFormat
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:
-
Make sure to rename any
Excel
namespace references in the code to the new namespaceExcelDataReader
-
Make sure the project has a reference to the
ExcelDataReader.DataSet
package to useAsDataSet()
-
Remove the line of code with
IsFirstRowAsColumnNames
and change the call to AsDataSet() to something like this:
var result = reader.AsDataSet(new ExcelDataSetConfiguration() { ConfigureDataTable = (_) => new ExcelDataTableConfiguration() { 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 package System.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.
Cross-platform C library for reading values from and writing values to .xlsx files.
Description
XLSX I/O aims to provide a C library for reading and writing .xlsx files.
The .xlsx file format is the native format used by Microsoft(R) Excel(TM) since version 2007.
Goal
The library was written with the following goals in mind:
- written in standard C, but allows being used by C++
- simple interface
- small footprint
- portable across different platforms (Windows, *nix)
- minimal dependancies: only depends on expat (only for reading) and minizip or libzip (which in turn depend on zlib)
- separate library for reading and writing .xlsx files
- does not require Microsoft(R) Excel(TM) to be installed
Reading .xlsx files:
- intended to process .xlsx files as a data table, which assumes the following:
- assumes the first row contains header names
- assumes the next rows contain values in the same columns as where the header names are supplied
- only values are processed, anything else is ignored (formulas, layout, graphics, charts, …)
- the entire shared string table is loaded in memory (warning: could be large for big spreadsheets with a lot of different values)
- supports .xlsx files without shared string table
- worksheet data itself is read on the fly without the need to buffer data in memory
- 2 methods are provided
- a simple method that allows the application to iterate trough rows and cells
- an advanced method (with less overhead) which calls callback functions for each cell and after each row
Writing .xlsx files:
- intended for writing data tables as .xlsx files, which assumes the following:
- only support for writing data (no support for formulas, layout, graphics, charts, …)
- no support for multiple worksheets (only one worksheet per file)
- on the fly file generation without the need to buffer data in memory
- no support for shared strings (all values are written as inline strings)
Libraries
The following libraries are provided:
-lxlsxio_read
— library for reading .xlsx files, requires#include <xlsxio_read.h>
-lxlsxio_write
— library for writing .xlsx files, requires#include <xlsxio_write.h>
-lxlsxio_readw
— experimental library for reading .xlsx files, linked with -lexpatw, requires#define XML_UNICODE
before#include <xlsxio_read.h>
Command line utilities
Some command line utilities are included:
xlsxio_xlsx2csv
— converts all sheets in all specified .xlsx files to individual CSV (Comma Separated Values) files.xlsxio_csv2xlsx
— converts all specified CSV (Comma Separated Values) files to .xlsx files.
Dependancies
This project has the following depencancies:
- expat (only for libxlsxio_read)
- minizip or libzip (libxlsxio_read and libxlsxio_write)
Note that minizip is preferred, as there have been reports that .xlsx files generated with XLSX I/O built against libzip can’t be opened with LibreOffice.
There is no dependancy on Microsoft(R) Excel(TM).
XLSX I/O was written with cross-platform portability in mind and works on multiple operating systems, including Windows, macOS and Linux.
Building from source
Requirements:
- a C compiler like gcc or clang, on Windows MinGW and MinGW-w64 are supported
- the dependancy libraries (see Dependancies)
- a shell environment, on Windows MSYS is supported
- the make command
- CMake version 2.6 or higher (optional, but preferred)
There are 2 methods to build XLSX I/O:
- using the basic Makefile included
- using CMake (preferred)
Building with make
- build and install by running
make install
optionally followed by:PREFIX=<path>
— Base path were files will be installed (defaults to /usr/local)WITH_LIBZIP=1
— Use libzip instead of minizipWIDE=1
— Also build UTF-16 library (xlsxio_readw)STATICDLL=1
— Build a static DLL (= doesn’t depend on any other DLLs) — only supported on Windows
Building with CMake (preferred method)
- configure by running
cmake -G"Unix Makefiles"
(orcmake -G"MSYS Makefiles"
on Windows) optionally followed by:-DCMAKE_INSTALL_PREFIX:PATH=<path>
Base path were files will be installed-DBUILD_STATIC:BOOL=OFF
— Don’t build static libraries-DBUILD_SHARED:BOOL=OFF
— Don’t build shared libraries-DBUILD_TOOLS:BOOL=OFF
— Don’t build tools (only libraries)-DBUILD_EXAMPLES:BOOL=OFF
— Don’t build examples-DWITH_LIBZIP:BOOL=ON
— Use libzip instead of Minizip-DWITH_WIDE:BOOL=ON
— Also build UTF-16 library (libxlsxio_readw)
- build and install by running
make install
(ormake install/strip
to strip symbols)
For Windows prebuilt binaries are also available for download (both 32-bit and 64-bit)
Example C program
//open .xlsx file for reading
xlsxioreader xlsxioread;
if ((xlsxioread = xlsxioread_open(filename)) == NULL) {
fprintf(stderr, "Error opening .xlsx filen");
return 1;
}
//list available sheets
xlsxioreadersheetlist sheetlist;
const char* sheetname;
printf("Available sheets:n");
if ((sheetlist = xlsxioread_sheetlist_open(xlsxioread)) != NULL) {
while ((sheetname = xlsxioread_sheetlist_next(sheetlist)) != NULL) {
printf(" - %sn", sheetname);
}
xlsxioread_sheetlist_close(sheetlist);
}
//read values from first sheet
char* value;
printf("Contents of first sheet:n");
xlsxioreadersheet sheet = xlsxioread_sheet_open(xlsxioread, NULL, XLSXIOREAD_SKIP_EMPTY_ROWS);
while (xlsxioread_sheet_next_row(sheet)) {
while ((value = xlsxioread_sheet_next_cell(sheet)) != NULL) {
printf("%st", value);
free(value);
}
printf("n");
}
xlsxioread_sheet_close(sheet);
//clean up
xlsxioread_close(xlsxioread);
License
XLSX I/O is released under the terms of the MIT License (MIT), see LICENSE.txt.
This means you are free to use XLSX I/O in any of your projects, from open source to commercial.
This library does not require Microsoft(R) Excel(TM) to be installed.
Excel 2007 stores the data in a bunch of files, most of them in XML, all crammed together into a zip file. If you want to look at the contents, you can rename your .xlsx
to whatever.zip
and then open it and look at the files inside.
Assuming your Excel file just contains raw data, and all you care about is reading it (i.e., you do not need/want to update its contents and get Excel to open it again), reading the data is actually pretty easy. Inside the zip file, you’re looking for the subdirectory xlworksheets
, which will contain a number of .xml
files, one for each worksheet from Excel (e.g., a default workbook will have three worksheets named sheet1.xml
, sheet2.xml
and sheet3.xml
).
Inside of those, you’re looking for the <sheet data>
tag. Inside of that, you’ll have <row>
tags (one for each row of data), and inside of them <c>
tags with an attribute r=RC
where RC
is replaced by the normal row/column notation (e.g., «A1»). The <c>
tag will have nested <v>
tag where you’ll find the value for that cell.
I do feel obliged to add a warning though: while reading really simple data can indeed be just this easy, life can get a lot more complex in a hurry if you decide to do much more than reading simple rows/columns of numbers. Trying to do anything even slightly more complex than that can get a lot more complex in a hurry.
C# Read Excel File with Examples
This tutorial explains how to read an Excel file in C#, as well as perform everyday tasks like data validation, database conversion, Web API intergrations, and formula modification. This article references code examples that utilize the IronXL .NET Excel library.
IronXL facilitates reading and editing Microsoft Excel documents with C#. IronXL neither requires Microsoft Excel nor does it require Interop. In fact, IronXL provides a faster and more intuitive API than Microsoft.Office.Interop.Excel
.
IronXL Includes:
- Dedicated product support from our .NET engineers
- Easy installation via Microsoft Visual Studio
- 30 day free trial test for development. Licenses from $749.
Reading and creating Excel files in C# and VB.NET is easy using the IronXL software library.
Overview
How to Read Excel File in C#
- Download the C# Library to read Excel files
- Load and read an Excel file (workbook)
- Create an Excel workbook in CSV or XLSX
- Edit cell values in a range of cells
- Validate spreadsheet data
- Export data using Entity Framework
Reading .XLS and .XLSX Excel Files Using IronXL
Below is a summary of the overal workflow for reading Excel files using IronXL:
- Install the IronXL Excel Library. We can do this using our NuGet package or by downloading the .Net Excel DLL.
- Use the
WorkBook.Load
method to read any XLS, XLSX or CSV document. - Get Cell values using intuitive syntax:
sheet["A11"].DecimalValue
:path=/static-assets/excel/content-code-examples/tutorials/how-to-read-excel-file-csharp-1.cs
using IronXL;
using System.Linq;
using System;
// Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
WorkBook workBook = WorkBook.Load("test.xlsx");
WorkSheet workSheet = workBook.WorkSheets.First();
// Select cells easily in Excel notation and return the calculated value
int cellValue = workSheet["A2"].IntValue;
// Read from Ranges of cells elegantly.
foreach (var cell in workSheet["A2:A10"])
{
Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text);
}
// Advanced Operations
// Calculate aggregate values such as Min, Max and Sum
decimal sum = workSheet["A2:A10"].Sum();
// Linq compatible
decimal max = workSheet["A2:A10"].Max(c => c.DecimalValue);
Imports IronXL
Imports System.Linq
Imports System
' Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
Private workBook As WorkBook = WorkBook.Load("test.xlsx")
Private workSheet As WorkSheet = workBook.WorkSheets.First()
' Select cells easily in Excel notation and return the calculated value
Private cellValue As Integer = workSheet("A2").IntValue
' Read from Ranges of cells elegantly.
For Each cell In workSheet("A2:A10")
Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text)
Next cell
' Advanced Operations
' Calculate aggregate values such as Min, Max and Sum
Dim sum As Decimal = workSheet("A2:A10").Sum()
' Linq compatible
Dim max As Decimal = workSheet("A2:A10").Max(Function(c) c.DecimalValue)
VB C#
The code examples used in the next sections of this tutorial (along with the sample project code) will work on three sample Excel spreadsheets (see below for a visual):
Tutorial
1. Download the IronXL C# Library for FREE
Install with NuGet
Install-Package IronXL.Excel
nuget.org/packages/IronXL.Excel/
or
Download DLL
The first thing we need to do is install the IronXL.Excel library, adding Excel functionality to the .NET framework.
Installing IronXL.Excel, is most easily achieved using our NuGet package, although you may also choose to manually install the DLL to your project or to your global assembly cache.
Installing the IronXL NuGet Package
- In Visual Studio, right-click on the project select «Manage NuGet Packages …»
-
Search for the IronXL.Excel package and click on the Install button to add it to the project
Another way to install the IronXL library is using the NuGet Package Manager Console:
- Enter the Package Manager Console
-
Type > Install-Package IronXL.Excel
PM > Install-Package IronXL.Excel
Additionally, you can view the package on the NuGet website
Manual Installation
Alternatively, we can start by downloading the IronXL .NET Excel DLL and manually installing into Visual Studio.
2. Load an Excel Workbook
The WorkBook
class represents an Excel sheet. To open an Excel File using C#, we use WorkBook.Load
method, specifying the path of the Excel fil.
:path=/static-assets/excel/content-code-examples/tutorials/how-to-read-excel-file-csharp-2.cs
WorkBook workBook = WorkBook.Load(@"Spreadsheets\GDP.xlsx");
Dim workBook As WorkBook = WorkBook.Load("Spreadsheets\GDP.xlsx")
VB C#
Sample: ExcelToDBProcessor
Each WorkBook
can have multiple WorkSheet
objects. Each one represents a single Excel worksheet in the Excel document. Use the WorkBook.GetWorkSheet
method to retrieve a reference to a specific Excel worksheet.
:path=/static-assets/excel/content-code-examples/tutorials/how-to-read-excel-file-csharp-3.cs
Sample: ExcelToDB
Creating new Excel Documents
To create a new Excel document, construct a new WorkBook
object with a valid file type.
:path=/static-assets/excel/content-code-examples/tutorials/how-to-read-excel-file-csharp-4.cs
WorkBook workBook = new WorkBook(ExcelFileFormat.XLSX);
Dim workBook As New WorkBook(ExcelFileFormat.XLSX)
VB C#
Sample: ApiToExcelProcessor
Note: Use ExcelFileFormat.XLS
toi support legacy versions of Microsoft Excel (95 and earlier).
Add a Worksheet to an Excel Document
As explained previously, an IronXL WorkBook
contains a collection of one or more WorkSheet
s.
This is how one workbook with two worksheets looks in Excel.
To create a new WorkSheet call WorkBook.CreateWorkSheet
with the name of the worksheet.
:path=/static-assets/excel/content-code-examples/tutorials/how-to-read-excel-file-csharp-5.cs
WorkSheet workSheet = workBook.GetWorkSheet("GDPByCountry");
Dim workSheet As WorkSheet = workBook.GetWorkSheet("GDPByCountry")
VB C#
3. Access Cell Values
Read and Edit a Single Cell
Access to the values of individual spreadsheet cells is carried out by retrieving the desired cell from its WorkSheet
. as shown below:
:path=/static-assets/excel/content-code-examples/tutorials/how-to-read-excel-file-csharp-16.cs
WorkBook workBook = WorkBook.Load("test.xlsx");
WorkSheet workSheet = workBook.DefaultWorksheet();
IronXL.Cell cell = workSheet["B1"];
Dim workBook As WorkBook = WorkBook.Load("test.xlsx")
Dim workSheet As WorkSheet = workBook.DefaultWorksheet()
Dim cell As IronXL.Cell = workSheet("B1")
VB C#
IronXL’s Cell
class represents an invidual cell in an Excel spreadsheet. It contains properties and methods that enable users to access and modify the cell’s value directly.
Each WorkSheet
object manages an index of Cell
objects corresponding to every cell value in an Excel worksheet. In the source code above, we reference the desired cell by its row and column index (cell B1 in this case) using standard array indexing syntax.
With a reference to Cell object, we can read and write data to and from a spreadsheet cell:
:path=/static-assets/excel/content-code-examples/tutorials/how-to-read-excel-file-csharp-17.cs
IronXL.Cell cell = workSheet["B1"];
string value = cell.StringValue; // Read the value of the cell as a string
Console.WriteLine(value);
cell.Value = "10.3289"; // Write a new value to the cell
console.WriteLine(cell.StringValue);
Dim cell As IronXL.Cell = workSheet("B1")
Dim value As String = cell.StringValue ' Read the value of the cell as a string
Console.WriteLine(value)
cell.Value = "10.3289" ' Write a new value to the cell
console.WriteLine(cell.StringValue)
VB C#
Read and Write a Range of Cell Values
The Range
class represents a two-dimensional collection of Cell
objects. This collection refers to a literal range of Excel cells. Obtain ranges by using the string indexer on a WorkSheet
object.
The argument text is either the coordinate of a cell (e.g. «A1», as shown previously) or a span of cells from left to right top to bottom (e.g. «B2:E5»). It is also possible to call GetRange
on a WorkSheet
.
:path=/static-assets/excel/content-code-examples/tutorials/how-to-read-excel-file-csharp-6.cs
Range range = workSheet["D2:D101"];
Dim range As Range = workSheet("D2:D101")
VB C#
Sample: DataValidation
There are several ways to read or edit the values of cells within a Range. If the count is known, use a For loop.
:path=/static-assets/excel/content-code-examples/tutorials/how-to-read-excel-file-csharp-7.cs
// Iterate through the rows
for (var y = 2; y <= 101; y++)
{
var result = new PersonValidationResult { Row = y };
results.Add(result);
// Get all cells for the person
var cells = workSheet[$"A{y}:E{y}"].ToList();
// Validate the phone number (1 = B)
var phoneNumber = cells[1].Value;
result.PhoneNumberErrorMessage = ValidatePhoneNumber(phoneNumberUtil, (string)phoneNumber);
// Validate the email address (3 = D)
result.EmailErrorMessage = ValidateEmailAddress((string)cells[3].Value);
// Get the raw date in the format of Month Day[suffix], Year (4 = E)
var rawDate = (string)cells[4].Value;
result.DateErrorMessage = ValidateDate(rawDate);
}
' Iterate through the rows
For y = 2 To 101
Dim result = New PersonValidationResult With {.Row = y}
results.Add(result)
' Get all cells for the person
Dim cells = workSheet($"A{y}:E{y}").ToList()
' Validate the phone number (1 = B)
Dim phoneNumber = cells(1).Value
result.PhoneNumberErrorMessage = ValidatePhoneNumber(phoneNumberUtil, CStr(phoneNumber))
' Validate the email address (3 = D)
result.EmailErrorMessage = ValidateEmailAddress(CStr(cells(3).Value))
' Get the raw date in the format of Month Day[suffix], Year (4 = E)
Dim rawDate = CStr(cells(4).Value)
result.DateErrorMessage = ValidateDate(rawDate)
Next y
VB C#
Sample: DataValidation
Add Formula to a Spreadsheet
Set formula of Cell
s with the Formula
property.
The code below iterates through each state and puts a percentage total in column C.
:path=/static-assets/excel/content-code-examples/tutorials/how-to-read-excel-file-csharp-13.cs
// Iterate through all rows with a value
for (var y = 2; y < i; y++)
{
// Get the C cell
Cell cell = workSheet[$"C{y}"].First();
// Set the formula for the Percentage of Total column
cell.Formula = $"=B{y}/B{i}";
}
' Iterate through all rows with a value
Dim y = 2
Do While y < i
' Get the C cell
Dim cell As Cell = workSheet($"C{y}").First()
' Set the formula for the Percentage of Total column
cell.Formula = $"=B{y}/B{i}"
y += 1
Loop
VB C#
Sample: AddFormulaeProcessor
Validate Spreadsheet Data
Use IronXL to validate a sheet of data. The DataValidation sample uses libphonenumber-csharp
to validate phone numbers and uses standard C# APIs to validate email addresses and dates.
:path=/static-assets/excel/content-code-examples/tutorials/how-to-read-excel-file-csharp-8.cs
// Iterate through the rows
for (var i = 2; i <= 101; i++)
{
var result = new PersonValidationResult { Row = i };
results.Add(result);
// Get all cells for the person
var cells = worksheet[$"A{i}:E{i}"].ToList();
// Validate the phone number (1 = B)
var phoneNumber = cells[1].Value;
result.PhoneNumberErrorMessage = ValidatePhoneNumber(phoneNumberUtil, (string)phoneNumber);
// Validate the email address (3 = D)
result.EmailErrorMessage = ValidateEmailAddress((string)cells[3].Value);
// Get the raw date in the format of Month Day[suffix], Year (4 = E)
var rawDate = (string)cells[4].Value;
result.DateErrorMessage = ValidateDate(rawDate);
}
' Iterate through the rows
For i = 2 To 101
Dim result = New PersonValidationResult With {.Row = i}
results.Add(result)
' Get all cells for the person
Dim cells = worksheet($"A{i}:E{i}").ToList()
' Validate the phone number (1 = B)
Dim phoneNumber = cells(1).Value
result.PhoneNumberErrorMessage = ValidatePhoneNumber(phoneNumberUtil, CStr(phoneNumber))
' Validate the email address (3 = D)
result.EmailErrorMessage = ValidateEmailAddress(CStr(cells(3).Value))
' Get the raw date in the format of Month Day[suffix], Year (4 = E)
Dim rawDate = CStr(cells(4).Value)
result.DateErrorMessage = ValidateDate(rawDate)
Next i
VB C#
The above code loops through each row in the spreadsheet and grabs the cells as a list. Each validates method checks the value of a cell and returns an error message if the value is invalid.
This code creates a new sheet, specifies headers, and outputs the error message results so that there is a log of invalid data.
:path=/static-assets/excel/content-code-examples/tutorials/how-to-read-excel-file-csharp-9.cs
var resultsSheet = workBook.CreateWorkSheet("Results");
resultsSheet["A1"].Value = "Row";
resultsSheet["B1"].Value = "Valid";
resultsSheet["C1"].Value = "Phone Error";
resultsSheet["D1"].Value = "Email Error";
resultsSheet["E1"].Value = "Date Error";
for (var i = 0; i < results.Count; i++)
{
var result = results[i];
resultsSheet[$"A{i + 2}"].Value = result.Row;
resultsSheet[$"B{i + 2}"].Value = result.IsValid ? "Yes" : "No";
resultsSheet[$"C{i + 2}"].Value = result.PhoneNumberErrorMessage;
resultsSheet[$"D{i + 2}"].Value = result.EmailErrorMessage;
resultsSheet[$"E{i + 2}"].Value = result.DateErrorMessage;
}
workBook.SaveAs(@"Spreadsheets\PeopleValidated.xlsx");
Dim resultsSheet = workBook.CreateWorkSheet("Results")
resultsSheet("A1").Value = "Row"
resultsSheet("B1").Value = "Valid"
resultsSheet("C1").Value = "Phone Error"
resultsSheet("D1").Value = "Email Error"
resultsSheet("E1").Value = "Date Error"
For i = 0 To results.Count - 1
Dim result = results(i)
resultsSheet($"A{i + 2}").Value = result.Row
resultsSheet($"B{i + 2}").Value = If(result.IsValid, "Yes", "No")
resultsSheet($"C{i + 2}").Value = result.PhoneNumberErrorMessage
resultsSheet($"D{i + 2}").Value = result.EmailErrorMessage
resultsSheet($"E{i + 2}").Value = result.DateErrorMessage
Next i
workBook.SaveAs("Spreadsheets\PeopleValidated.xlsx")
VB C#
4. Export Data using Entity Framework
Use IronXL to export data to a database or convert an Excel spreadsheet to a database. The ExcelToDB
sample reads a spreadsheet with GDP by country and then exports that data to an SQLite.
It uses EntityFramework to build the database and then export the data line by line.
Add the SQLite Entity Framework NuGet packages.
EntityFramework allows you to create a model object that can export data to the database.
:path=/static-assets/excel/content-code-examples/tutorials/how-to-read-excel-file-csharp-10.cs
public class Country
{
[Key]
public Guid Key { get; set; }
public string Name { get; set; }
public decimal GDP { get; set; }
}
Public Class Country
<Key>
Public Property Key() As Guid
Public Property Name() As String
Public Property GDP() As Decimal
End Class
VB C#
To use a different database, install the corresponding NuGet package and find the equivalent of UseSqLite()
:path=/static-assets/excel/content-code-examples/tutorials/how-to-read-excel-file-csharp-11.cs
public class CountryContext : DbContext
{
public DbSet<Country> Countries { get; set; }
public CountryContext()
{
//TODO: Make async
Database.EnsureCreated();
}
/// <summary>
/// Configure context to use Sqlite
/// </summary>
/// <param name="optionsBuilder"></param>
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var connection = new SqliteConnection($"Data Source=Country.db");
connection.Open();
var command = connection.CreateCommand();
//Create the database if it doesn't already exist
command.CommandText = $"PRAGMA foreign_keys = ON;";
command.ExecuteNonQuery();
optionsBuilder.UseSqlite(connection);
base.OnConfiguring(optionsBuilder);
}
}
Public Class CountryContext
Inherits DbContext
Public Property Countries() As DbSet(Of Country)
Public Sub New()
'TODO: Make async
Database.EnsureCreated()
End Sub
''' <summary>
''' Configure context to use Sqlite
''' </summary>
''' <param name="optionsBuilder"></param>
Protected Overrides Sub OnConfiguring(ByVal optionsBuilder As DbContextOptionsBuilder)
Dim connection = New SqliteConnection($"Data Source=Country.db")
connection.Open()
Dim command = connection.CreateCommand()
'Create the database if it doesn't already exist
command.CommandText = $"PRAGMA foreign_keys = ON;"
command.ExecuteNonQuery()
optionsBuilder.UseSqlite(connection)
MyBase.OnConfiguring(optionsBuilder)
End Sub
End Class
VB C#
Create a CountryContext
, iterate through the range to create each record, and then SaveAsync
to commit data to the database
:path=/static-assets/excel/content-code-examples/tutorials/how-to-read-excel-file-csharp-12.cs
public async Task ProcessAsync()
{
//Get the first worksheet
var workbook = WorkBook.Load(@"Spreadsheets\GDP.xlsx");
var worksheet = workbook.GetWorkSheet("GDPByCountry");
//Create the database connection
using (var countryContext = new CountryContext())
{
//Iterate through all the cells
for (var i = 2; i <= 213; i++)
{
//Get the range from A-B
var range = worksheet[$"A{i}:B{i}"].ToList();
//Create a Country entity to be saved to the database
var country = new Country
{
Name = (string)range[0].Value,
GDP = (decimal)(double)range[1].Value
};
//Add the entity
await countryContext.Countries.AddAsync(country);
}
//Commit changes to the database
await countryContext.SaveChangesAsync();
}
}
Public Async Function ProcessAsync() As Task
'Get the first worksheet
Dim workbook = WorkBook.Load("Spreadsheets\GDP.xlsx")
Dim worksheet = workbook.GetWorkSheet("GDPByCountry")
'Create the database connection
Using countryContext As New CountryContext()
'Iterate through all the cells
For i = 2 To 213
'Get the range from A-B
Dim range = worksheet($"A{i}:B{i}").ToList()
'Create a Country entity to be saved to the database
Dim country As New Country With {
.Name = CStr(range(0).Value),
.GDP = CDec(CDbl(range(1).Value))
}
'Add the entity
Await countryContext.Countries.AddAsync(country)
Next i
'Commit changes to the database
Await countryContext.SaveChangesAsync()
End Using
End Function
VB C#
Sample: ExcelToDB
5. Download Data from an API to Spreadsheet
The following call makes a REST call with RestClient.Net. It downloads JSON and converts it into a «List» of the type RestCountry
. It is then easy to iterate through each country and save the data from the REST API to an Excel spreadsheet.
:path=/static-assets/excel/content-code-examples/tutorials/how-to-read-excel-file-csharp-14.cs
var client = new Client(new Uri("https://restcountries.eu/rest/v2/"));
List<RestCountry> countries = await client.GetAsync<List<RestCountry>>();
Dim client As New Client(New Uri("https://restcountries.eu/rest/v2/"))
Dim countries As List(Of RestCountry) = Await client.GetAsync(Of List(Of RestCountry))()
VB C#
Sample: ApiToExcel
This is what the API JSON data looks like.
The following code iterates through the countries and sets the Name, Population, Region, NumericCode, and Top 3 Languages in the spreadsheet.
:path=/static-assets/excel/content-code-examples/tutorials/how-to-read-excel-file-csharp-15.cs
for (var i = 2; i < countries.Count; i++)
{
var country = countries[i];
//Set the basic values
workSheet[$"A{i}"].Value = country.name;
workSheet[$"B{i}"].Value = country.population;
workSheet[$"G{i}"].Value = country.region;
workSheet[$"H{i}"].Value = country.numericCode;
//Iterate through languages
for (var x = 0; x < 3; x++)
{
if (x > (country.languages.Count - 1)) break;
var language = country.languages[x];
//Get the letter for the column
var columnLetter = GetColumnLetter(4 + x);
//Set the language name
workSheet[$"{columnLetter}{i}"].Value = language.name;
}
}
For i = 2 To countries.Count - 1
Dim country = countries(i)
'Set the basic values
workSheet($"A{i}").Value = country.name
workSheet($"B{i}").Value = country.population
workSheet($"G{i}").Value = country.region
workSheet($"H{i}").Value = country.numericCode
'Iterate through languages
For x = 0 To 2
If x > (country.languages.Count - 1) Then
Exit For
End If
Dim language = country.languages(x)
'Get the letter for the column
Dim columnLetter = GetColumnLetter(4 + x)
'Set the language name
workSheet($"{columnLetter}{i}").Value = language.name
Next x
Next i
VB C#
Object Reference and Resources
You may also find the IronXL class documentation within the Object Reference of great value.
In addition, there are other tutorials which may shed light in other aspects of IronXL.Excel including Creating, Opening, Writing, Editing, Saving and Exporting XLS, XLSX and CSV files without using Excel Interop.
Summary
IronXL.Excel is alone .NET software library for reading a wide variety of spreadsheet formats. It does not require Microsoft Excel to be installed, and is not dependant on Interop.
Tutorial Quick Access
Download this Tutorial as C# Source Code
The full free C# for Excel Source Code for this tutorial is available to download as a zipped Visual Studio 2017 project file.
Download
Explore this Tutorial on GitHub
The source code for this project is available in C# and VB.NET on GitHub.
Use this code as an easy way to get up and running in just a few minutes. The project is saved as a Microsoft Visual Studio 2017 project, but is compatible with any .NET IDE.
How to Read Excel File in C# on GitHub
View the API Reference
Explore the API Reference for IronXL, outlining the details of all of IronXL’s features, namespaces, classes, methods fields and enums.
View the API Reference
Christian Findlay
Software Development Team Lead
Christian builds software for the health industry and leads up a team. Christian has years of experience integrating with systems of all kinds. IronXL allows Christian to import and manipulate data from different sources to automate repetitive tasks and validate input data from 3rd party sources.
All programmers need to learn how to read excel files. This is the information age, and many people store their data in excel files because they are user-friendly. Programmers must also know all about reading excel files in their respective programming language.
If you are a .Net Programmer and struggling with reading excel files, then don’t worry — you are in the right place. I will show you a step-by-step for reading excel files using C#.
You might be thinking that reading excel files would be difficult and require high-level expertise, but this is not the case. I will show you that it only takes 2-3 lines of code with IronXL to be able to read excel files.
We will be covering the following topics in this article:
- Introduction to IronXL
- Creating an Example Project for our Demonstration
-
Adding IronXL Nuget Package to our Project
a. Installing IronXL using the Package Manager Console
b. Installing IronXL using the Manage Package Manager
c. Directly download it and add a reference to the project -
Read the Excel file in C#
a. Designing the Form
b. Code to Read the data from Excel
c. Code to Read data within a specific range
d. Code to Read XLs and XLSX File
e. Code to Read CSV files -
Summary
Introduction to IronXL:
IronXL is a .Net Library development brought to you by Iron Software. This library provides excellent functions and APIs to help us read, create and update/edit our excel files and spreadsheets. IronXL does not require Excel to be installed on your server or Interop. Moreover, IronXL provides a faster and more intuitive API than Microsoft Office Interop Excel.
IronXL works on .Net Core 2, Framework 4.5, Azure, Mono and, Mobile and Xamarin.
Creating an Example Project for our Demonstration
I will be using Windows Form App as an example, but you can use one of your choice, such as Asp.Net Web App, .Net core MVC or .Net Core web api.
To create a new project, open Visual Studio, click on «Create New Project» => Select Template; I am using the Windows Form App, you can use any of your choosing. => Click on Next Button => Name Your Project; I have named mine as «C Sharp Read Excel File». => Click on Next = > Select your Target Framework; I am selecting .Net core 3.1, but again, you can choose your own. => Finally, click on «Create Project», and a project will be created and opened for you as shown below:
Our next task is to Install the Nuget Package for IronXL.
(adsbygoogle = window.adsbygoogle || []).push({});
Adding the IronXL Nuget Package
We can add the IronXL Package in one of any three ways, so you can choose the method that suits you best.
Installing IronXL using the Package Manager Console
Open the Package Manager console in your Project and use the following command:
Go to Tools => NuGet Package Manager => Package Manager Console.
This will open the Package Manager console for you. Next, write the following command in the Package Manager console:
PM > Install-Package IronXL.Excel
Installing IronXL using the Nuget Package Manager
This is another way to install the Nuget Package Manager. If you have already completed installation using the previous method, than you won’t need to use this one.
Go to: Tools = > NuGet Package Manager => Manage NuGet Packages for Solution, and click on it.
This will open the Nuget-Solution for you; click on «Browse» and search for IronXL.Excel in the search bar:
Click on the «Install» button and it will install IronXL for you. After installing IronXL, go to your form and start designing it.
Downloading IronXL
Another way of using IronXL is to download it directly from this link. Once downloaded, just add the reference of the file in your project and start using it.
Design the Form
Now, we have to design the form to suit our requirements. I will use the minimum design for the demonstration. You can use any design depending on your needs.
Go to the ToolBox=> Select Label (for name our example app), and select the «Panel and Data» grid view. Put the «Data» grid view inside the panel. Our design will look like the following:
Our form has now been designed. Our next task is to actually write the code for reading an excel file or excel sheet in C#.
Write Code for Reading Excel Files in C
We want our app to read any given excel file at the click of a button, so for this, double-click on the «Read Excel File» button, and the following code will appear:
private void button1_Click(object sender, EventArgs e)
{
}
Enter fullscreen mode
Exit fullscreen mode
Next, add the following namespace in this file:
using System;
using System.Windows.Forms;
using IronXL;
using System.Data;
Enter fullscreen mode
Exit fullscreen mode
Using the IronXL namespace is necessary to access the IronXL functions. However, you can use other namespaces as per the requirements of your own projects.
WorkBook workbook = WorkBook.Load("Weather.xlsx");
WorkSheet sheet = workbook.GetWorkSheet("Sheet1");
DataTable dt = sheet.ToDataTable(true);
dataGridView1.DataSource = dt;
Enter fullscreen mode
Exit fullscreen mode
The first statement will load the excel workbook «Weather.xlsx» for us. I have chosen the xlsx file format as an example. You can choose any file of your choice.
After loading the excel workbook, we need to select the excel worksheet we want to read. After that, we need to convert our worksheet into a data table so that we can further load it into our Data Grid View.
This will read the file for us and display all its data in the data grid view.
Output for Reading the Data from Excel File using C#:
Let’s open our «Weather.xlsx» file in Microsoft Excel and compare our output. xlsx file is the extension for a Microsoft Excel file.
Excel File:
Now that we can read excel files using C#, let’s explore some advanced options.
Let’s suppose that you want to read the data from an excel file for a specific cell range. Let’s look at how to do that.
Read Data from Excel within a Specific Cell Range:
To read the data in a specific cell range, write the following code behind the button:
WorkBook workbook = WorkBook.Load("Weather.xlsx");
WorkSheet sheet = workbook.GetWorkSheet("Sheet1");
var range = sheet["A1:C6"];
DataTable dt = range.ToDataTable(true);
dataGridView1.DataSource = dt;
Enter fullscreen mode
Exit fullscreen mode
The first two lines are the same as those for loading the workbook and selecting the worksheet we want to read. On the third line, we have selected the cell range within which we want to read the data. I have selected the range A1:C6 which means that it will read the first three columns: Column A, Column B, and Column C, as well as the first six rows, as shown below.
Output for Read Data within a Specific Cell Range:
Read XLS or XLSX File:
We can read excel files no matter whether the file format is xls or xlsx. In the previous example, we looked at the example of reading the data from an Xslx file. Next, let’s look at saving the same file in .xls format and then reading it. The code will remain the same, we just have to change the file name to «Wather.xls» as shown below:
WorkBook workbook = WorkBook.Load("Weather.xls");
Output for Reading XLS File:
Read CSV File in C#:
If you have csv files instead of an excel file, you have to make slight changes to the code.
Let’s look at the following example.
I have the same Weather file saved as «Weather.csv», so I am using the same file for all the examples. Again, you can use any example according to your requirements.
WorkBook workbook = WorkBook.LoadCSV("Weather.csv", fileFormat: ExcelFileFormat.XLSX, ListDelimiter: ",");
WorkSheet sheet = workbook.DefaultWorkSheet;
DataTable dt = sheet.ToDataTable(true);
dataGridView1.DataSource = dt;
Enter fullscreen mode
Exit fullscreen mode
On the first line will load the csv file for you and define the file format. The second line will select the worksheet, while the other two code lines are the same as those in the previous examples.
Output for Reading CSV File
Summary:
In this tutorial we have learned how to read data using excel in C# by selecting specific cells and ranges, or different file formats. This all was done with just a few lines of code.
If you want to learn more about Iron Software products, please click on this link. If you decide that you’d like to try it out for yourself, then go ahead and take advantage of the Iron Software 30-day Free-Trial . You may also want to take a look at the Iron Suite, this complete package includes five .Net Libraries, this special offer means you can get all five products for the price of only two. For more information, please click here.
I hope that you have found this guide helpful and easy to follow. If you need any further assistance, please feel free to post a comment.