Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
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.WorkSheet["A1:B10"]
).To use IronXL in C# applications, install the following components on your local computer:
Once Visual Studio and IronXL are installed, add the necessary IronXL namespaces by including the following line at the top of your C# file:
// Add reference to the IronXL library
using IronXL;
// Add reference to the IronXL library
using IronXL;
' Add reference to the IronXL library
Imports IronXL
Excel files, also known as workbooks, consist of multiple worksheets, each containing cell values. To open and read an Excel file, load it 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 initializes the workbook as a WorkBook
instance. To open a specific WorkSheet
, retrieve it from the WorkSheets
collection:
// Access the first worksheet in the workbook
WorkSheet sheet = workbook.WorkSheets.First();
// Access the first worksheet in the workbook
WorkSheet sheet = workbook.WorkSheets.First();
' Access the first worksheet in the workbook
Dim sheet As WorkSheet = workbook.WorkSheets.First()
This accesses the first sheet in the Excel file, ready for reading and writing.
Excel File
Once the Excel file is opened, it is ready for reading data. Reading data from Excel files in C# using IronXL is straightforward. You can read cell values by specifying the cell reference.
The following code retrieves the value of a cell:
// Select the cell using Excel notation and retrieve its integer value
int cellValue = sheet["C2"].IntValue;
// Display the value in the console
Console.WriteLine(cellValue);
// Select the cell using Excel notation and retrieve its integer value
int cellValue = sheet["C2"].IntValue;
// Display the value in the console
Console.WriteLine(cellValue);
' Select the cell using Excel notation and retrieve its integer value
Dim cellValue As Integer = sheet("C2").IntValue
' Display the value in the console
Console.WriteLine(cellValue)
The output is as follows:
Read Excel
To read data from a range of cells, use a loop to iterate through the specified range:
// Iterate through a range of cells and display their address and text content
foreach (var cell in sheet["A2:A6"])
{
Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text);
}
// Iterate through a range of cells and display their address and text content
foreach (var cell in sheet["A2:A6"])
{
Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text);
}
' Iterate through a range of cells and display their address and text content
For Each cell In sheet("A2:A6")
Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text)
Next cell
Each value in the cell range A2:A6
is accessed and printed to the console.
Read Range of Cells
For more detailed reading and writing examples, check the Excel reading tutorial in C#.
IronXL also facilitates creating new workbooks for data saving and retrieving.
You can create a new Excel file with a single line of code:
// Create a new workbook with the XLSX format
WorkBook workBook = new WorkBook(ExcelFileFormat.XLSX);
// Create a new workbook with the XLSX format
WorkBook workBook = new WorkBook(ExcelFileFormat.XLSX);
' Create a new workbook with the XLSX format
Dim workBook As New WorkBook(ExcelFileFormat.XLSX)
Next, create a worksheet and add data to it.
// Create a worksheet named "GDPByCountry" in the workbook
WorkSheet workSheet = workBook.CreateWorkSheet("GDPByCountry");
// Create a worksheet named "GDPByCountry" in the workbook
WorkSheet workSheet = workBook.CreateWorkSheet("GDPByCountry");
' Create a worksheet named "GDPByCountry" in the workbook
Dim workSheet As WorkSheet = workBook.CreateWorkSheet("GDPByCountry")
This code adds a worksheet named "GDPByCountry" to the workbook, allowing you to add cell values.
To set a value for a specific cell, use the code below:
// Set the value of cell A1 to "Example"
workSheet["A1"].Value = "Example";
// Set the value of cell A1 to "Example"
workSheet["A1"].Value = "Example";
' Set the value of cell A1 to "Example"
workSheet("A1").Value = "Example"
The final output is:
Add Value to Cell
This article demonstrates how to open and read Excel files, such as XLS and XLSX, in C# using IronXL. IronXL doesn't require Microsoft Excel to be installed on the system for Excel-related tasks.
IronXL provides a comprehensive solution for Excel-related tasks programmatically, including formula calculation, string sorting, trimming, finding and replacing, merging and unmerging, saving files, and more. 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.
IronXL is a .NET library that allows users to open, read, create, and edit Excel files efficiently without using MS Office Interop. It is compatible with all .NET Frameworks as well as Linux, MacOS, Docker, Azure, and AWS, making it a powerful tool for developers.
IronXL supports multiple Excel file formats, including XLS, XLSX, CSV, and TSV. It can open, read, and write data in these formats, providing flexibility in handling various types of Excel files.
No, Microsoft Excel is not required to be installed on your system when using IronXL. The library operates independently of MS Office Interop, allowing you to work with Excel files without needing Excel installed.
To open an Excel file using IronXL, you can use the `WorkBook.Load` method to load the file. This initializes the file as a `WorkBook` instance, from which you can access sheets and cells.
Yes, IronXL can be used to create new Excel workbooks. You can create a workbook using the `WorkBook` class and add worksheets and data to it programmatically.
You can read cell values by specifying the cell reference in Excel notation. For example, `sheet["C2"].IntValue` retrieves the integer value of cell C2.
Yes, IronXL is compatible with all .NET Frameworks, Linux, MacOS, Docker, Azure, and AWS, making it versatile and suitable for various development environments.
Key features of IronXL include opening, reading, and editing Excel files, exporting worksheets to various formats, encrypting/decrypting files, recalculating formulas, and editing spreadsheets with intuitive syntax.
To use IronXL, install Visual Studio and the IronXL library via NuGet or download the .NET Excel DLL. Add the IronXL namespace to your C# file to begin working with Excel files.
Yes, IronXL offers a free 30-day trial, allowing you to explore its features before purchasing a commercial license.