C# Read Excel File Example

It's important for us as developers to be able to process a large amount of Excel data and evaluate the results in the right formats. We need the quickest and easiest method to easily read CSharp Excel data from a spreadsheet and apply it to our applications. Here we outline multiple C# Read Excel File Example projects and how to use IronXL to your advantage.

Read Cell Values
using IronXL;
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    //get value by cell address
    string address_val = ws["B3"].ToString();
    //get value by row and column indexing
    string index_val = ws.Rows[5].Columns[1].ToString();
    Console.WriteLine("Get value by Cell Addressing:\n Value of cell B3: {0}", address_val);
    Console.WriteLine("Get value by Row and Column index:\n Value of Row 5 and Column 1: {0}", index_val);
    Console.ReadKey();
}
Imports Microsoft.VisualBasic
Imports IronXL
Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	'get value by cell address
	Dim address_val As String = ws("B3").ToString()
	'get value by row and column indexing
	Dim index_val As String = ws.Rows(5).Columns(1).ToString()
	Console.WriteLine("Get value by Cell Addressing:" & vbLf & " Value of cell B3: {0}", address_val)
	Console.WriteLine("Get value by Row and Column index:" & vbLf & " Value of Row 5 and Column 1: {0}", index_val)
	Console.ReadKey()
End Sub
Jump to Article
Read Data Range
using IronXL;
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    //get specified range values by loop
    foreach (var item in ws["B3:B8"])
    {
        Console.WriteLine("Value is: {0}", item);
    }
    Console.ReadKey();
}
Imports IronXL
Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	'get specified range values by loop
	For Each item In ws("B3:B8")
		Console.WriteLine("Value is: {0}", item)
	Next item
	Console.ReadKey()
End Sub
Jump to Article
Read Boolean Data
using IronXL;
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    foreach (var item in ws["G1:G10"])
    {
    Console.WriteLine("  Condition is: {0}", item.BoolValue);
    }
    Console.ReadKey();
}
Imports IronXL
Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	For Each item In ws("G1:G10")
	Console.WriteLine("  Condition is: {0}", item.BoolValue)
	Next item
	Console.ReadKey()
End Sub
Jump to Article
Read Complete Worksheet
using IronXL;
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    //Traverse all rows of Excel WorkSheet
    for (int i = 0;  i< ws.Rows.Count(); i++)
    {
        //Traverse all columns of specific Row
        for (int j = 0; j < ws.Columns.Count(); j++)
        {
            //Get the values
            string val = ws.Rows[i].Columns[j].Value.ToString();
            Console.WriteLine("Value of Row {0} and Column {1} is: {2}", i, j,val);
        }
    }
    Console.ReadKey();
}
Imports IronXL
Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	'Traverse all rows of Excel WorkSheet
	For i As Integer = 0 To ws.Rows.Count() - 1
		'Traverse all columns of specific Row
		For j As Integer = 0 To ws.Columns.Count() - 1
			'Get the values
			Dim val As String = ws.Rows(i).Columns(j).Value.ToString()
			Console.WriteLine("Value of Row {0} and Column {1} is: {2}", i, j,val)
		Next j
	Next i
	Console.ReadKey()
End Sub
Jump to Article
Try IronXL free for development


Step 1

1. Install the Library

Install the Library via DLL Download or access it through the NuGet page. This IronXL library will give you full access to the functions you need for reading Excel file data, and you can use it for unlimited developerment in your project.

 PM > Install-Package IronXL.Excel


How To Tutorial

2. Open the WorkSheet

Let's get started with how to read Excel file data in CSharp projects by loading an Excel file and the desired worksheet in our project.

//Load Excel file
WorkBook wb = WorkBook.Load("Path");
//Load Excel file
WorkBook wb = WorkBook.Load("Path");
'Load Excel file
Dim wb As WorkBook = WorkBook.Load("Path")
VB   C#

The above code will create an object, wb, of WorkBook class and load the specified Excel file in it. After this, we can open any WorkSheet of specified Excel file as follows:

//Open Excel WorkSheet
WorkSheet ws = wb.GetWorkSheet("SheetName");
//Open Excel WorkSheet
WorkSheet ws = wb.GetWorkSheet("SheetName");
'Open Excel WorkSheet
Dim ws As WorkSheet = wb.GetWorkSheet("SheetName")
VB   C#

wb is a WorkBook which is defined in the above code sample. The specified WorkSheet is opened in ws and we can use it to get any type of data, in many ways.

Now, let's look at some examples to understand how to work with IronXL to read the data from Excel Spreadsheets.


3. Read Cell Value of Excel File

If we want to read just the specific cell values of an Excel file, then we would use the WorkSheet[] function in IronXL.

string val = WorkSheet["Cell Address"].ToString();
string val = WorkSheet["Cell Address"].ToString();
Dim val As String = WorkSheet("Cell Address").ToString()
VB   C#

This will return the data of a specific cell address. There is also another way to read specific cell data from an Excel file, using row and column indexing.

string val = WorkSheet.Rows[RowIndex].Columns[ColumnIndex].ToString();
string val = WorkSheet.Rows[RowIndex].Columns[ColumnIndex].ToString();
Dim val As String = WorkSheet.Rows(RowIndex).Columns(ColumnIndex).ToString()
VB   C#

Note: The row and column index starts from 0.

Let’s see the example of how to get data from Excel file using the two methods above.

using IronXL;
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    //get value by cell address
    string address_val = ws["B3"].ToString();
    //get value by row and column indexing
    string index_val = ws.Rows[5].Columns[1].ToString();
    Console.WriteLine("Get value by Cell Addressing:\n Value of cell B3: {0}", address_val);
    Console.WriteLine("Get value by Row and Column index:\n Value of Row 5 and Column 1: {0}", index_val);
    Console.ReadKey();
}
using IronXL;
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    //get value by cell address
    string address_val = ws["B3"].ToString();
    //get value by row and column indexing
    string index_val = ws.Rows[5].Columns[1].ToString();
    Console.WriteLine("Get value by Cell Addressing:\n Value of cell B3: {0}", address_val);
    Console.WriteLine("Get value by Row and Column index:\n Value of Row 5 and Column 1: {0}", index_val);
    Console.ReadKey();
}
Imports Microsoft.VisualBasic
Imports IronXL
Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	'get value by cell address
	Dim address_val As String = ws("B3").ToString()
	'get value by row and column indexing
	Dim index_val As String = ws.Rows(5).Columns(1).ToString()
	Console.WriteLine("Get value by Cell Addressing:" & vbLf & " Value of cell B3: {0}", address_val)
	Console.WriteLine("Get value by Row and Column index:" & vbLf & " Value of Row 5 and Column 1: {0}", index_val)
	Console.ReadKey()
End Sub
VB   C#

The above code display the following output:

And we can observe the Excel file sample.xlsx values here:

It's pretty simple to read data from Excel files with IronXL, which is great to save us time and process energy. For more examples on how to read data from Excel files we can dive into accessing Excel cell values here.


4. Read Excel Data in a Range

IronXL provides an intelligent way to read Excel file data in a specific range. The range can be applied for both rows and columns.

//Apply range
WorkSheet["From Cell Address : To Cell Address"];
//Apply range
WorkSheet["From Cell Address : To Cell Address"];
'Apply range
WorkSheet("From Cell Address : To Cell Address")
VB   C#

It will return all the values from the specified range. Let's see the example of how to get Excel data in a specific range using IronXL.

using IronXL;
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    //get specified range values by loop
    foreach (var item in ws["B3:B8"])
    {
        Console.WriteLine("Value is: {0}", item);
    }
    Console.ReadKey();
}
using IronXL;
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    //get specified range values by loop
    foreach (var item in ws["B3:B8"])
    {
        Console.WriteLine("Value is: {0}", item);
    }
    Console.ReadKey();
}
Imports IronXL
Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	'get specified range values by loop
	For Each item In ws("B3:B8")
		Console.WriteLine("Value is: {0}", item)
	Next item
	Console.ReadKey()
End Sub
VB   C#

The above code displays the following output:

And produces the Excel file sample.xlsx values:

Read more about working with a CSharp Excel Range and file data.


5. Read Boolean Data of Excel File

In application development, we need to make decisions based on the Boolean data type of the Excel file. For this purpose, we need to use the BoolValue() function of IronXL as follow;

WorkSheet["Cell Address"].BoolValue;
WorkSheet["Cell Address"].BoolValue;
WorkSheet("Cell Address").BoolValue
VB   C#

It will return a boolean data type True or False. Let's see the example of how to read a boolean datatype of Excel file in a specified range:

using IronXL;
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    foreach (var item in ws["G1:G10"])
    {
    Console.WriteLine("  Condition is: {0}", item.BoolValue);
    }
    Console.ReadKey();
}
using IronXL;
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    foreach (var item in ws["G1:G10"])
    {
    Console.WriteLine("  Condition is: {0}", item.BoolValue);
    }
    Console.ReadKey();
}
Imports IronXL
Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	For Each item In ws("G1:G10")
	Console.WriteLine("  Condition is: {0}", item.BoolValue)
	Next item
	Console.ReadKey()
End Sub
VB   C#

From this we get the output:

And the Excel file sample.xlsx with values from C1 to C10:


6. Read Complete Excel WorkSheet

It is simple to read a complete Excel WorkSheet by using rows and columns indexes. For this purpose, we use two loops, one is for traversing all rows, and the second is for traversing all columns of a specific row, and then we can easily get all cell values of the whole Excel WorkSheet. Let's understand it with the help of an example that implements it:

using IronXL;
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    //Traverse all rows of Excel WorkSheet
    for (int i = 0;  i< ws.Rows.Count(); i++)
    {
        //Traverse all columns of specific Row
        for (int j = 0; j < ws.Columns.Count(); j++)
        {
            //Get the values
            string val = ws.Rows[i].Columns[j].Value.ToString();
            Console.WriteLine("Value of Row {0} and Column {1} is: {2}", i, j,val);
        }
    }
    Console.ReadKey();
}
using IronXL;
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    //Traverse all rows of Excel WorkSheet
    for (int i = 0;  i< ws.Rows.Count(); i++)
    {
        //Traverse all columns of specific Row
        for (int j = 0; j < ws.Columns.Count(); j++)
        {
            //Get the values
            string val = ws.Rows[i].Columns[j].Value.ToString();
            Console.WriteLine("Value of Row {0} and Column {1} is: {2}", i, j,val);
        }
    }
    Console.ReadKey();
}
Imports IronXL
Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	'Traverse all rows of Excel WorkSheet
	For i As Integer = 0 To ws.Rows.Count() - 1
		'Traverse all columns of specific Row
		For j As Integer = 0 To ws.Columns.Count() - 1
			'Get the values
			Dim val As String = ws.Rows(i).Columns(j).Value.ToString()
			Console.WriteLine("Value of Row {0} and Column {1} is: {2}", i, j,val)
		Next j
	Next i
	Console.ReadKey()
End Sub
VB   C#

The output of the above code will display all the values of the Excel WorkSheet.


Tutorial Quick Access

Documentation Resource

The documentation resource is available for everyone in the IronXL API Reference, including a guide to all the functions, features, classes, namespaces, and enums at your disposal.

Documentation Resource