Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
This tutorial will use IronXL as a main tool to open and read Excel files in C# projects.
IronXL is a .NET library that prioritizes ease of use, accuracy and speed for its users. It helps you open, read, create, and edit Excel files with lightning-fast performance and without any errors. It works without MS Office Interop, which makes it a powerful tool for developers.
IronXL is compatible with all .NET Frameworks along with Linux, MacOS, Docker, Azure and AWS. It can be used to create Console, Web and Desktop Applications such as Blazor and MAUI for modern Web Apps. It supports different workbook formats like XLS and XLSX files, XSLT and XLSM, CSV and TSV.
System.Data.DataSet
and System.Data.DataTable
objects.To use IronXL in C# applications, those components are needed to install on your local computer:
Once Visual Studio and IronXL are installed, IronXL namepsaces
are needed to reference in the source code. Add the following line of code on top of the file within a new project where IronXL functions will be used:
//add reference
using IronXL;
//add reference
using IronXL;
'add reference
Imports IronXL
Excel files are also known as workbooks. Each workbook contains multiple worksheets, and each worksheet contains cell values. To open and read an Excel file, it should be loaded using the WorkBook
class's Load
method.
//Supported Excel spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
WorkBook workbook = WorkBook.Load("test.xlsx");
//Supported Excel spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
WorkBook workbook = WorkBook.Load("test.xlsx");
'Supported Excel spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
Dim workbook As WorkBook = WorkBook.Load("test.xlsx")
This opens the Excel file in WorkBook
instance reference variable. It can have multiple worksheets, so it can be used to open a specific WorkSheet
or all at once. The following code opens the first WorkSheet
in the sheet instance variable:
WorkSheet sheet = workbook.WorkSheets.First();
WorkSheet sheet = workbook.WorkSheets.First();
Dim sheet As WorkSheet = workbook.WorkSheets.First()
This will open the first sheet in an Excel file and now data can be read and written to this sheet.
The Excel file opened here is:
Excel File
Once the Excel file is opened, it is ready for reading data. Reading data from Excel files in C# using IronXL is very easy and simple. You can read cell values by simply mentioning the cell reference number.
The following code retrieves the value of a cell from the file:
//Select cells easily in Excel-notation and return the value
int cellValue = sheet ["C2"].IntValue;
//Display the value
Console.WriteLine(cellValue);
//Select cells easily in Excel-notation and return the value
int cellValue = sheet ["C2"].IntValue;
//Display the value
Console.WriteLine(cellValue);
'Select cells easily in Excel-notation and return the value
Dim cellValue As Integer = sheet ("C2").IntValue
'Display the value
Console.WriteLine(cellValue)
The output is as follows:
Read Excel
Now, let's read data from a range of cells in the opened Excel file. The code goes as follows:
// Read from Range of cells elegantly.
foreach (var cell in sheet ["A2:A6"])
{
Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text);
}
// Read from Range of cells elegantly.
foreach (var cell in sheet ["A2:A6"])
{
Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text);
}
' Read from Range of cells elegantly.
For Each cell In sheet ("A2:A6")
Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text)
Next cell
The value within the cell within cell range A2 - A6 is accessed using a for loop. Each value is printed to the console.
Read Range of Cells
For a detailed work of reading and writing to cell values, check the read Excel file in another C# example tutorial.
IronXL also helps in creating new workbooks, which can later be used for data saving and retrieving.
Just like loading the Excel files, creating Excel files is also very easy and can be done using only one line of code.
WorkBook workBook = new WorkBook(ExcelFileFormat.XLSX);
WorkBook workBook = new WorkBook(ExcelFileFormat.XLSX);
Dim workBook As New WorkBook(ExcelFileFormat.XLSX)
Let's create a worksheet and add some data to it for later use.
WorkSheet workSheet = workBook.CreateWorkSheet("GDPByCountry");
WorkSheet workSheet = workBook.CreateWorkSheet("GDPByCountry");
Dim workSheet As WorkSheet = workBook.CreateWorkSheet("GDPByCountry")
The line of code shown above creates a worksheet named "GDPByCountry" in the currently loaded workbook and now can add cell values into the current WorkSheet
.
The following code adds a value to A1
cell:
workSheet ["A1"].Value = "Example";
workSheet ["A1"].Value = "Example";
workSheet ("A1").Value = "Example"
The final output is as follows:
Add Value to Cell
This article demonstrated how to open and read Excel files such as XLS, XLSX etc in C# using IronXL. IronXL does not require Microsoft Excel to be installed on the system to perform all Excel-related tasks.
IronXL provides a solution for all Excel-related tasks to be done programmatically whether it be formula calculation, string sorting, trimming, finding and replacing, merging and unmerging, saving files etc. You can also set cell data formats.
IronXL is available for a free 30-day trial and can be licensed for commercial use. IronXL's Lite package starts from $749.
9 .NET API products for your office documents