C# Read Excel File Example
Published: 2022-02-11 07:44:03

---
canonical_url: https://ironsoftware.com/csharp/excel/docs/questions/c-sharp-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.

---

## 1. Install the Library

[Install the Library via DLL Download](https://ironsoftware.com/csharp/excel/packages/IronXL.Package.For.read.excel.example.zip) or [access it through the NuGet page](https://www.nuget.org/packages/IronXL.Excel). 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.

**C#:**
```
//Load Excel file
WorkBook wb = WorkBook.Load("Path");
```

**VB:**
```
'Load Excel file
Dim wb As WorkBook = WorkBook.Load("Path")
```

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:

**C#:**
```
//Open Excel WorkSheet
WorkSheet ws = wb.GetWorkSheet("SheetName");
```

**VB:**
```
'Open Excel WorkSheet
Dim ws As WorkSheet = wb.GetWorkSheet("SheetName")
```

`wb` is a WorkBook that 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.

**C#:**
```
string val = WorkSheet["Cell Address"].ToString();
```

**VB:**
```
Dim val As String = WorkSheet("Cell Address").ToString()
```

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.

**C#:**
```
string val = WorkSheet.Rows[RowIndex].Columns[ColumnIndex].ToString();
```

**VB:**
```
Dim val As String = WorkSheet.Rows(RowIndex).Columns(ColumnIndex).ToString()
```

**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.

**C#:**
```
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();
}
```

**VB:**
```
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
```

The above code display the following output:

![Image 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j2pmnfz2r1p1wqk6he4q.png)
 
And we can observe the Excel file `sample.xlsx` values here:

![Image 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7n3n2bpmmi7nz9uceot3.png)
 
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](https://ironsoftware.com/csharp/excel/#read-excel) 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.

**C#:**
```
//Apply range
WorkSheet["From Cell Address : To Cell Address"];
```

**VB:**
```
'Apply range
WorkSheet("From Cell Address : To Cell Address")
```

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.

**C#:**
```
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();
}
```

**VB:**
```
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
```

The above code displays the following output:

![Image 3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mpvgz3acwch42d5fm0d7.png)
 
And produces the Excel file `sample.xlsx` values:

![Image 4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f4lrc1krercc4i9o3rwn.png)
 
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;

**C#:**
```
WorkSheet["Cell Address"].BoolValue;
```

**VB:**
```
WorkSheet("Cell Address").BoolValue
```

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:

**C#:**
```
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();
}
```

**VB:**
```
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
```

From this we get the output:

![Image 5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0drwfgucb5b1918m8ulm.png)
 
And the Excel file `sample.xlsx` with values from `C1` to `C10`:

![Image 6](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h14xnwnppj9vbqn5h7o0.png)
 
---

## 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:

**C#:**
```
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();
}
```

**VB:**
```
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
```

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

---