DEV Community

IronSoftware
IronSoftware

Posted on • Originally published at ironsoftware.com

2 1

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.


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.

C#:

//Load Excel file
WorkBook wb = WorkBook.Load("Path");
Enter fullscreen mode Exit fullscreen mode

VB:

'Load Excel file
Dim wb As WorkBook = WorkBook.Load("Path")
Enter fullscreen mode Exit fullscreen mode

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");
Enter fullscreen mode Exit fullscreen mode

VB:

'Open Excel WorkSheet
Dim ws As WorkSheet = wb.GetWorkSheet("SheetName")
Enter fullscreen mode Exit fullscreen mode

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();
Enter fullscreen mode Exit fullscreen mode

VB:

Dim val As String = WorkSheet("Cell Address").ToString()
Enter fullscreen mode Exit fullscreen mode

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();
Enter fullscreen mode Exit fullscreen mode

VB:

Dim val As String = WorkSheet.Rows(RowIndex).Columns(ColumnIndex).ToString()
Enter fullscreen mode Exit fullscreen mode

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();
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

The above code display the following output:

Image 1

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

Image 2

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.

C#:

//Apply range
WorkSheet["From Cell Address : To Cell Address"];
Enter fullscreen mode Exit fullscreen mode

VB:

'Apply range
WorkSheet("From Cell Address : To Cell Address")
Enter fullscreen mode Exit fullscreen mode

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();
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

The above code displays the following output:

Image 3

And produces the Excel file sample.xlsx values:

Image 4

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;
Enter fullscreen mode Exit fullscreen mode

VB:

WorkSheet("Cell Address").BoolValue
Enter fullscreen mode Exit fullscreen mode

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();
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

From this we get the output:

Image 5

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

Image 6


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();
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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


Neon image

Build better on Postgres with AI-Assisted Development Practices

Compare top AI coding tools like Cursor and Windsurf with Neon's database integration. Generate synthetic data and manage databases with natural language.

Read more →

Top comments (0)

Jetbrains image

Build Secure, Ship Fast

Discover best practices to secure CI/CD without slowing down your pipeline.

Read more

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay