C# Open Excel Worksheets
Learn how to use C# to open Excel Worksheet functions to work with Excel Spreadsheets and open all file types including (.xls
, .csv
, .tsv
, and .xlsx
). Opening an Excel Worksheet, reading its data, and manipulating it programmatically are essential for many 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 File in C#
- Install C# library to open Excel files
- Load the Excel file into WorkBook object
- Explore many ways to select WorkSheet from the opened Excel file
- Access cell data via the selected WorkSheet object
- Get data from rows and columns range
How to Open Excel Worksheet in C#
- Install the Excel library to read Excel files.
- Load the existing Excel file into a
Workbook
object. - Set the default Excel Worksheet.
- Read the value from Excel Workbook.
- 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 in 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:
:path=/static-assets/excel/content-code-examples/how-to/c-sharp-open-excel-worksheet-load-workbook.cs
// Load the Excel file into a WorkBook object
WorkBook workBook = WorkBook.Load("Path_to_Excel_File.xlsx"); // Excel file path
' Load the Excel file into a WorkBook object
Dim workBook As WorkBook = WorkBook.Load("Path_to_Excel_File.xlsx") ' Excel file path
The Excel file at the specified path will load into the workBook
object. Now, we need to specify the Excel Worksheet which will be opened.
3. Open Excel WorkSheet
To open a specific WorkSheet
of an Excel file, IronXL provides the WorkBook.GetWorkSheet()
function. Using this, we can easily open the Worksheet by its name:
:path=/static-assets/excel/content-code-examples/how-to/c-sharp-open-excel-worksheet-get-worksheet.cs
// Get a worksheet by its name
WorkSheet workSheet = workBook.GetWorkSheet("SheetName");
' Get a worksheet by its name
Dim workSheet As WorkSheet = workBook.GetWorkSheet("SheetName")
The specified WorkSheet
will open in workSheet
with all its data. There are also a few other ways to open a specific WorkSheet
of an Excel file:
:path=/static-assets/excel/content-code-examples/how-to/c-sharp-open-excel-worksheet-multiple-open.cs
using IronXL;
using System.Linq;
// Open by sheet index
WorkSheet workSheet = workBook.WorkSheets[0];
// Open the default worksheet
WorkSheet workSheet2 = workBook.DefaultWorkSheet;
// Open the first sheet
WorkSheet workSheet3 = workBook.WorkSheets.First();
// Open the first or default sheet
WorkSheet workSheet4 = workBook.WorkSheets.FirstOrDefault();
Imports IronXL
Imports System.Linq
' Open by sheet index
Private workSheet As WorkSheet = workBook.WorkSheets(0)
' Open the default worksheet
Private workSheet2 As WorkSheet = workBook.DefaultWorkSheet
' Open the first sheet
Private workSheet3 As WorkSheet = workBook.WorkSheets.First()
' Open the first or default sheet
Private workSheet4 As WorkSheet = workBook.WorkSheets.FirstOrDefault()
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:
- Get a specific cell value of Excel
WorkSheet
. - Get data in a specific Range.
- 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:
:path=/static-assets/excel/content-code-examples/how-to/c-sharp-open-excel-worksheet-cell-address.cs
// Access a specific cell value by its address
string val = workSheet["Cell Address"].ToString();
' Access a specific cell value by its address
Dim val As String = workSheet("Cell Address").ToString()
workSheet
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."
:path=/static-assets/excel/content-code-examples/how-to/c-sharp-open-excel-worksheet-cell-row.cs
// Access a cell value by row index and column index
string val = workSheet.Rows[RowIndex].Columns[ColumnIndex].Value.ToString();
' Access a cell value by row index and column index
Dim val As String = workSheet.Rows(RowIndex).Columns(ColumnIndex).Value.ToString()
Let's see an example of how to open an Excel file in our C# project and get specific cell values using both methods:
:path=/static-assets/excel/content-code-examples/how-to/c-sharp-open-excel-worksheet-specified-cell.cs
using IronXL;
using System;
WorkBook workBook = WorkBook.Load("sample.xlsx");
// Open WorkSheet
WorkSheet workSheet = workBook.GetWorkSheet("Sheet1");
// Get value By Cell Address
int intValue = workSheet["C6"].Int32Value;
// Get value by Row and Column Address
string strValue = workSheet.Rows[3].Columns[1].Value.ToString();
Console.WriteLine("Getting Value by Cell Address: {0}", intValue);
Console.WriteLine("Getting Value by Row and Column Indexes: {0}", strValue);
Imports IronXL
Imports System
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
' Open WorkSheet
Private workSheet As WorkSheet = workBook.GetWorkSheet("Sheet1")
' Get value By Cell Address
Private intValue As Integer = workSheet("C6").Int32Value
' Get value by Row and Column Address
Private strValue As String = workSheet.Rows(3).Columns(1).Value.ToString()
Console.WriteLine("Getting Value by Cell Address: {0}", intValue)
Console.WriteLine("Getting Value by Row and Column Indexes: {0}", strValue)
This code displays the following output:
Value of Excel file sample.xlsx
in row [3].Column [1]
and C6
cell:
The rows and column indices start from 0
.
Open Excel WorkSheets
and get specific cell 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:
:path=/static-assets/excel/content-code-examples/how-to/c-sharp-open-excel-worksheet-select-range.cs
// Access data from a specific range
var rangeData = workSheet["From Cell Address : To Cell Address"];
' Access data from a specific range
Dim rangeData = workSheet("From Cell Address : To Cell Address")
Let's see an example of how to use range to get data from an open Excel WorkSheet
:
:path=/static-assets/excel/content-code-examples/how-to/c-sharp-open-excel-worksheet-from-range.cs
using IronXL;
using System;
// Load Excel file
WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.GetWorkSheet("Sheet1");
// Specify the range
foreach (var cell in workSheet["B2:B10"])
{
Console.WriteLine("Value is: {0}", cell.Text);
}
Imports IronXL
Imports System
' Load Excel file
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
Private workSheet As WorkSheet = workBook.GetWorkSheet("Sheet1")
' Specify the range
For Each cell In workSheet("B2:B10")
Console.WriteLine("Value is: {0}", cell.Text)
Next cell
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:
:path=/static-assets/excel/content-code-examples/how-to/c-sharp-open-excel-worksheet-select-row-range.cs
// Access data from a specific row range
var rowData = ws["A1:E1"];
' Access data from a specific row range
Dim rowData = ws("A1:E1")
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.
:path=/static-assets/excel/content-code-examples/how-to/c-sharp-open-excel-worksheet-all.cs
using IronXL;
using System;
using System.Linq;
// Load Excel file
WorkBook workBook = WorkBook.Load("sample2.xlsx");
WorkSheet workSheet = workBook.GetWorkSheet("Sheet1");
// Access all rows of the open Excel WorkSheet
for (int i = 0; i < workSheet.Rows.Count(); i++)
{
// Access all columns of a specific row
for (int j = 0; j < workSheet.Columns.Count(); j++)
{
// Access each cell for the specified column
Console.WriteLine(workSheet.Rows[i].Columns[j].Value.ToString());
}
}
Imports IronXL
Imports System
Imports System.Linq
' Load Excel file
Private workBook As WorkBook = WorkBook.Load("sample2.xlsx")
Private workSheet As WorkSheet = workBook.GetWorkSheet("Sheet1")
' Access all rows of the open Excel WorkSheet
For i As Integer = 0 To workSheet.Rows.Count() - 1
' Access all columns of a specific row
For j As Integer = 0 To workSheet.Columns.Count() - 1
' Access each cell for the specified column
Console.WriteLine(workSheet.Rows(i).Columns(j).Value.ToString())
Next j
Next i
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 ResourceFrequently Asked Questions
How can I open and manipulate Excel worksheets using C#?
You can open and manipulate Excel worksheets in C# by using the IronXL library. Start by installing the library, then load your Excel file into a WorkBook
object using WorkBook.Load()
. You can select and open specific worksheets with the WorkBook.GetWorkSheet()
method.
What Excel file types are supported in C#?
In C#, you can work with various Excel file types such as .xls
, .csv
, .tsv
, and .xlsx
using the IronXL library.
How do I retrieve a specific cell's value from an Excel worksheet in C#?
To retrieve a specific cell's value from an Excel worksheet using C#, utilize the IronXL library to access cells by their address with ws["Cell Address"].ToString()
or by specifying row and column indices using ws.Rows[RowIndex].Columns[ColumnIndex].Value.ToString()
.
Can I fetch data from a defined range of cells in a worksheet using C#?
Yes, you can fetch data from a defined range of cells in a worksheet using C# by employing the IronXL library's syntax ws["From Cell Address : To Cell Address"]
to access the data within the specified range.
How can I read all the data from an Excel worksheet programmatically?
To read all data from an Excel worksheet programmatically in C#, you can loop through each row and column using the IronXL library, accessing each cell's value with ws.Rows[i].Columns[j].Value.ToString()
.
Why should developers use a library for handling Excel files in C#?
Using a library like IronXL is beneficial for developers as it simplifies the process of opening, reading, and manipulating Excel files within C# projects, providing robust functions and classes for handling Excel content efficiently.
How can I install an Excel handling library in my C# project?
To install the IronXL library for Excel handling in your C# project, use the NuGet package manager with the command dotnet add package IronXL.Excel
.
Is it possible to open a worksheet by its index in a workbook using C#?
Yes, you can open a worksheet by its index in a workbook using the IronXL library with the syntax WorkSheet ws = wb.WorkSheets[index];
, where 'index' represents the worksheet's position in the workbook.
How can I work with specific cell ranges in an Excel file using C#?
IronXL allows you to work with specific cell ranges by specifying ranges such as "A1:E1"
to access and manipulate the data within that defined range in an Excel file.
Where can I find more information about the functions available for Excel handling in C#?
For more detailed information about the functions available for Excel handling in C#, you can refer to the IronXL API Reference resource, which provides comprehensive documentation on functions, classes, and namespaces. Visit the API Reference at https://ironsoftware.com/csharp/excel/object-reference/api/.