C# Open Excel Worksheets

Learn how to use C# open Excel Worksheet functions to work with Excel Spreadsheets and open all file types including (.xls, .csv, .tsv, and .xlsx). To open an Excel Worksheet, read its data, and manipulate it programmatically is essential for many who are developing applications. Here's a solution for every developer who wants a method with fewer lines of code and faster response times.


How to Open Excel Worksheet in C#

  1. Install the Excel library to read Excel files.
  2. Load the existing Excel file into a Workbook object.
  3. Set the default Excel Worksheet.
  4. Read the value from Excel Workbook.
  5. Process the value accordingly, and display.

Step 1

1. Access Excel C# Library

Access the Excel C# Library via DLL or install it using your preferred NuGet manager. Once you've accessed the IronXL library and added it to your project, you can use all the functions below to open Excel Worksheets C#.

Install-Package IronXL.Excel

How to Tutorial

2. Load Excel File

Use the WorkBook.Load() function from IronXL to load Excel files into the project. This function requires a string parameter, which is the path of the Excel file to be opened. See here:

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

The Excel file of the specified path will load in wb. Now, we need to specify the Excel Worksheet which will be opened.


3. Open Excel WorkSheet

For opening a specific WorkSheet of an Excel file, IronXL provides the WorkBook.GetWorkSheet() function. Using this, we can easily open the Worksheet by its name:

WorkSheet ws = WorkBook.GetWorkSheet("SheetName");
WorkSheet ws = WorkBook.GetWorkSheet("SheetName");
Dim ws As WorkSheet = WorkBook.GetWorkSheet("SheetName")
VB   C#

The specified WorkSheet will open in ws with all its data. There are also a few other ways to open a specific WorkSheet of an Excel file:

/**
Open Excel Worksheet
anchor-open-excel-worksheet
**/
//by sheet index
WorkSheet ws = wb.WorkSheets [0];
//for the default
WorkSheet ws = wb.DefaultWorkSheet; 
//for the first sheet: 
WorkSheet ws = wb.WorkSheets.First();
//for the first or default sheet:
WorkSheet ws = wb.WorkSheets.FirstOrDefault();
/**
Open Excel Worksheet
anchor-open-excel-worksheet
**/
//by sheet index
WorkSheet ws = wb.WorkSheets [0];
//for the default
WorkSheet ws = wb.DefaultWorkSheet; 
//for the first sheet: 
WorkSheet ws = wb.WorkSheets.First();
//for the first or default sheet:
WorkSheet ws = wb.WorkSheets.FirstOrDefault();
'''
'''Open Excel Worksheet
'''anchor-open-excel-worksheet
'''*
'by sheet index
Dim ws As WorkSheet = wb.WorkSheets (0)
'for the default
Dim ws As WorkSheet = wb.DefaultWorkSheet
'for the first sheet: 
Dim ws As WorkSheet = wb.WorkSheets.First()
'for the first or default sheet:
Dim ws As WorkSheet = wb.WorkSheets.FirstOrDefault()
VB   C#

Now, we just need to get data from the opened Excel WorkSheet.


4. Get Data from WorkSheet

We can get data from an opened Excel WorkSheet in the following ways:

  1. Get a specific cell value of Excel WorkSheet.
  2. Get data in a specific Range.
  3. Get all the data from WorkSheet.

Let's see one by one how to get data in different ways with these examples:

4.1. Get Specific Cell Value

The first approach to getting data from an Excel WorkSheet is to get the specific cell values. It can be accessed like this:

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

ws is the WorkSheet of the Excel file, as we will see in the following examples. Specific cell values can also be accessed by specifying "row index" and "column index."

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

Let's see an example of how to open an Excel file in our C# project and get specific cell values using both ways:

/**
Get Cell Value
anchor-get-specific-cell-value
**/
using IronXL;
static void Main(string [] args)
{
    //Load Excel file
    WorkBook wb = WorkBook.Load("sample.xlsx");
    //Open WorkSheet
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    //Get value By Cell Address
    Int32 int_val= ws ["C6"].Int32Value;
    //Get value by Row and Column Address
    string str_val=ws.Rows [3].Columns [1].Value.ToString();

    Console.WriteLine("Getting Value by Cell Address: {0}",int_val);
    Console.WriteLine("Getting Value by Row and Column Indexes: {0}",str_val);
    Console.ReadKey();
}
/**
Get Cell Value
anchor-get-specific-cell-value
**/
using IronXL;
static void Main(string [] args)
{
    //Load Excel file
    WorkBook wb = WorkBook.Load("sample.xlsx");
    //Open WorkSheet
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    //Get value By Cell Address
    Int32 int_val= ws ["C6"].Int32Value;
    //Get value by Row and Column Address
    string str_val=ws.Rows [3].Columns [1].Value.ToString();

    Console.WriteLine("Getting Value by Cell Address: {0}",int_val);
    Console.WriteLine("Getting Value by Row and Column Indexes: {0}",str_val);
    Console.ReadKey();
}
'''
'''Get Cell Value
'''anchor-get-specific-cell-value
'''*
Imports IronXL
Shared Sub Main(ByVal args() As String)
	'Load Excel file
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	'Open WorkSheet
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	'Get value By Cell Address
	Dim int_val As Int32= ws ("C6").Int32Value
	'Get value by Row and Column Address
	Dim str_val As String=ws.Rows (3).Columns (1).Value.ToString()

	Console.WriteLine("Getting Value by Cell Address: {0}",int_val)
	Console.WriteLine("Getting Value by Row and Column Indexes: {0}",str_val)
	Console.ReadKey()
End Sub
VB   C#

This code displays the following output:

Value of Excel file sample.xlsx in row [3].Column [1] and C6 cell:

The rows and column index starts from 0.

Open Excel WorkSheets and get the specific call data, and you can read more about how to read Excel data in C# from already open Excel Worksheets.

4.2. Get Data from Specific Range

Now let's see how to get data in a specific range from an opened Excel WorkSheet using IronXL.

IronXL provides an intelligent way to get data in a specific range. We just specify from to to values:

WorkSheet ["From Cell Address : To Cell Address"];
WorkSheet ["From Cell Address : To Cell Address"];
WorkSheet ("From Cell Address : To Cell Address")
VB   C#

Let's see an example of how to use range to get data from an open Excel WorkSheet:

/**
Get Data from Range
anchor-get-data-from-specific-range
**/
using IronXL;
static void Main(string [] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    //specify the range 
    foreach (var cell in ws ["B2:B10"])
    {
        Console.WriteLine("value is: {0}", cell.Text);
    }
    Console.ReadKey();
}
/**
Get Data from Range
anchor-get-data-from-specific-range
**/
using IronXL;
static void Main(string [] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    //specify the range 
    foreach (var cell in ws ["B2:B10"])
    {
        Console.WriteLine("value is: {0}", cell.Text);
    }
    Console.ReadKey();
}
'''
'''Get Data from Range
'''anchor-get-data-from-specific-range
'''*
Imports IronXL
Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	'specify the range 
	For Each cell In ws ("B2:B10")
		Console.WriteLine("value is: {0}", cell.Text)
	Next cell
	Console.ReadKey()
End Sub
VB   C#

The above code will pull data from B2 to B10 as follows:

We can see the values of the Excel file sample.xlsx, from B2 to B10:

4.3. Get Data from Row

We can also describe a range for a specific row. For example:

WorkSheet ["A1:E1"]
WorkSheet ["A1:E1"]
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'WorkSheet ["A1:E1"]
VB   C#

This will display all values from A1 to E1. Read more about C# Excel Ranges and how to work with different row and column identifications.

4.4. Get All Data from WorkSheet

Getting all the cell data from the open Excel WorkSheet is also easy using IronXL. For this task, we need to access each cell value by row and column indexes. Let's see the following example, in which we will traverse all WorkSheet cells and access its values.

In this example, basically two loops are working: one is for traversing each row of Excel WorkSheet and the other is for traversing each column of a specific row. In this way each cell value can be easily accessed.

/**
Get All Data
anchor-get-all-data-from-worksheet
**/
using IronXL;
static void Main(string [] args)
{
    WorkBook wb = WorkBook.Load("sample2.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    //access all rows of open Excel WorkSheet
    for (int i = 0; i < ws.Rows.Count(); i++)
    {    
        //access all columns of specific row
        for (int j = 0; j < ws.Columns.Count(); j++)
        {
            //Access each cell for specified column
            Console.WriteLine(ws.Rows [i].Columns [j].Value.ToString());
        }
    }
    Console.ReadKey();
}
/**
Get All Data
anchor-get-all-data-from-worksheet
**/
using IronXL;
static void Main(string [] args)
{
    WorkBook wb = WorkBook.Load("sample2.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    //access all rows of open Excel WorkSheet
    for (int i = 0; i < ws.Rows.Count(); i++)
    {    
        //access all columns of specific row
        for (int j = 0; j < ws.Columns.Count(); j++)
        {
            //Access each cell for specified column
            Console.WriteLine(ws.Rows [i].Columns [j].Value.ToString());
        }
    }
    Console.ReadKey();
}
'''
'''Get All Data
'''anchor-get-all-data-from-worksheet
'''*
Imports IronXL
Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Load("sample2.xlsx")
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	'access all rows of open Excel WorkSheet
	For i As Integer = 0 To ws.Rows.Count() - 1
		'access all columns of specific row
		For j As Integer = 0 To ws.Columns.Count() - 1
			'Access each cell for specified column
			Console.WriteLine(ws.Rows (i).Columns (j).Value.ToString())
		Next j
	Next i
	Console.ReadKey()
End Sub
VB   C#

The output of the above code will display each cell value of the complete open Excel WorkSheet.


Tutorial Quick Access

API Reference Resource

Use the IronXL API Reference resource as your guide to all functions and classes for use in your projects, as well as namespaces, method fields, enums and feature sets.

API Reference Resource