Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
This article will discuss how you can read Excel data in C# using IronXL, a lightweight and fast library that is one of the most widely used C# libraries to interact with Excel files.
IronXL is a C# library that allows developers to easily read, write, and manipulate Excel files without the need for Microsoft Excel to be installed on the machine. It is a powerful tool for working with Excel data or reading Excel files in C# and provides a simple and easy-to-use API for reading and writing Excel files.
IronXL is a C# Excel library that provides a wide range of features or Excel functionality for working with Excel data, including:
IronXL can read, write, and manipulate Excel files in a variety of formats, including XLS, XLSX, CSV, and XML. It also supports reading of password-protected Excel files and other advanced operations.
IronXL provides support for working with Excel charts, allowing developers to create and manipulate charts within Excel files.
IronXL provides support for saving and exporting Excel files in different formats.
IronXL supports various file formats, such as XLS, XLSX, CSV, and XML.
IronXL provides support for working with Excel formulas, allowing developers to calculate formulas within Excel files.
IronXL provides support for working with Excel cell styling, allowing developers to format Excel files professionally.
This tutorial will create a project using the C# programming language and Visual Studio 2019.
Creating a C# Console Application in Visual Studio
Choose a name for the new C# Console Application Project
For best results, choose the latest available version of the .NET Framework for your C# Console Project
Existing C# projects can also be used. Simply open the project and add the library. The following section will show how to install the Excel library IronXL.
There are two methods for downloading and installing the IronXL library:
Access the NuGet Package Manager Console by going to Tools > NuGet Package Manager > Package Manager Console****
In the Console, type the following command and press Enter.
Install-Package IronXL.Excel
The IronXL NuGet Package will start to install and after a minute or two, it will be ready to use.
The IronXL library may be installed directly using the NuGet Package Manager UI. To install IronXL, go through the following steps:
Access the NuGet Package Manager by going to Tools > NuGet Package Manager > Manage packages for Solution...
From the NuGet Package Manager submenu, click on Manage NuGet Packages for Solution to access the NuGet Package Manager
IronXL.Excel
package. It will automatically install the library.IronXL offers the ability to read Excel sheets programmatically without any software. Just create a C# .NET project and with just only few lines of code, you can read Excel files in your console.
Reading XLSX files in C# is quite easy using IronXL. The below example will discuss how reading a Microsoft Excel workbook using C# is a piece of cake.
This is an example of an XLSX file containing data from the range A1 to D5 for the following example.
The Excel File that will be used in this example
using IronXL;
using System;
using System.Linq;
WorkBook workBook = WorkBook.Load("test.xlsx");
WorkSheet workSheet = workBook.WorkSheets [0];
Console.Write(workSheet);
using IronXL;
using System;
using System.Linq;
WorkBook workBook = WorkBook.Load("test.xlsx");
WorkSheet workSheet = workBook.WorkSheets [0];
Console.Write(workSheet);
Imports IronXL
Imports System
Imports System.Linq
Private workBook As WorkBook = WorkBook.Load("test.xlsx")
Private workSheet As WorkSheet = workBook.WorkSheets (0)
Console.Write(workSheet)
The output created from the above code example
Using IronXL, you can read multiple Excel workbooks at the same time. In the source code below, multiple Excel files are read.
using IronXL;
using System;
using System.Linq;
// workbook 1
WorkBook workBook = WorkBook.Load("test.xlsx");
WorkSheet workSheet = workBook.WorkSheets [0];
Console.WriteLine(workSheet);
Console.WriteLine();
// workbook 2
WorkBook workBook2 = WorkBook.Load("Example2.Sheet0.csv");
WorkSheet workSheet2 = workBook2.WorkSheets [0];
Console.WriteLine(workSheet2);
Console.WriteLine();
using IronXL;
using System;
using System.Linq;
// workbook 1
WorkBook workBook = WorkBook.Load("test.xlsx");
WorkSheet workSheet = workBook.WorkSheets [0];
Console.WriteLine(workSheet);
Console.WriteLine();
// workbook 2
WorkBook workBook2 = WorkBook.Load("Example2.Sheet0.csv");
WorkSheet workSheet2 = workBook2.WorkSheets [0];
Console.WriteLine(workSheet2);
Console.WriteLine();
Imports IronXL
Imports System
Imports System.Linq
' workbook 1
Private workBook As WorkBook = WorkBook.Load("test.xlsx")
Private workSheet As WorkSheet = workBook.WorkSheets (0)
Console.WriteLine(workSheet)
Console.WriteLine()
' workbook 2
Dim workBook2 As WorkBook = WorkBook.Load("Example2.Sheet0.csv")
Dim workSheet2 As WorkSheet = workBook2.WorkSheets (0)
Console.WriteLine(workSheet2)
Console.WriteLine()
Outputting the contents of more than one Excel document
IronXL offers another breakthrough feature to read multiple sheets in a single C# .NET program. You can use this feature for side-by-side comparisons. The below example will read multiple Excel sheets.
using IronXL;
using System;
WorkBook workBook = WorkBook.Load("multiple.xlsx");
WorkSheet workSheet = workBook.WorkSheets [0];
WorkSheet workSheet1 = workBook.WorkSheets [1];
Console.WriteLine(workSheet);
Console.WriteLine();
Console.WriteLine(workSheet1);
Console.WriteLine();
using IronXL;
using System;
WorkBook workBook = WorkBook.Load("multiple.xlsx");
WorkSheet workSheet = workBook.WorkSheets [0];
WorkSheet workSheet1 = workBook.WorkSheets [1];
Console.WriteLine(workSheet);
Console.WriteLine();
Console.WriteLine(workSheet1);
Console.WriteLine();
Imports IronXL
Imports System
Private workBook As WorkBook = WorkBook.Load("multiple.xlsx")
Private workSheet As WorkSheet = workBook.WorkSheets (0)
Private workSheet1 As WorkSheet = workBook.WorkSheets (1)
Console.WriteLine(workSheet)
Console.WriteLine()
Console.WriteLine(workSheet1)
Console.WriteLine()
Outputting the contents of more than one worksheet in a single Excel File
Reading Excel files in C# can be a difficult task for those new to programming. However, by using a library such as IronXL, the process becomes much simpler and more manageable.
IronXL is a powerful C# Excel library that provides a wide range of features for working with multiple Excel files or multiple worksheets at the same time.
For more information on how to read Excel files using IronXL, please visit the Read Excel Spreadsheets Code Example.
IronXL is free for development purposes but requires a license for commercial use. View the Reading Excel File in C# Tutorial for more code examples and step-by-step instructions on how to create and read Excel files.
9 .NET API products for your office documents