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#
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:
// Load the Excel file into a WorkBook object
WorkBook wb = WorkBook.Load("Path_to_Excel_File.xlsx"); // Excel file path
// Load the Excel file into a WorkBook object
WorkBook wb = WorkBook.Load("Path_to_Excel_File.xlsx"); // Excel file path
' Load the Excel file into a WorkBook object
Dim wb As WorkBook = WorkBook.Load("Path_to_Excel_File.xlsx") ' Excel file path
The Excel file at the specified path will load into the wb
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:
// Get a worksheet by its name
WorkSheet ws = wb.GetWorkSheet("SheetName");
// Get a worksheet by its name
WorkSheet ws = wb.GetWorkSheet("SheetName");
' Get a worksheet by its name
Dim ws As WorkSheet = wb.GetWorkSheet("SheetName")
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 by various methods
// Open by sheet index
WorkSheet ws = wb.WorkSheets[0];
// Open the default worksheet
WorkSheet ws = wb.DefaultWorkSheet;
// Open the first sheet
WorkSheet ws = wb.WorkSheets.First();
// Open the first or default sheet
WorkSheet ws = wb.WorkSheets.FirstOrDefault();
// Open Excel Worksheet by various methods
// Open by sheet index
WorkSheet ws = wb.WorkSheets[0];
// Open the default worksheet
WorkSheet ws = wb.DefaultWorkSheet;
// Open the first sheet
WorkSheet ws = wb.WorkSheets.First();
// Open the first or default sheet
WorkSheet ws = wb.WorkSheets.FirstOrDefault();
' Open Excel Worksheet by various methods
' Open by sheet index
Dim ws As WorkSheet = wb.WorkSheets(0)
' Open the default worksheet
Dim ws As WorkSheet = wb.DefaultWorkSheet
' Open the first sheet
Dim ws As WorkSheet = wb.WorkSheets.First()
' Open the first or default sheet
Dim ws As WorkSheet = wb.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:
// Access a specific cell value by its address
string val = ws["Cell Address"].ToString();
// Access a specific cell value by its address
string val = ws["Cell Address"].ToString();
' Access a specific cell value by its address
Dim val As String = ws("Cell Address").ToString()
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."
// Access a cell value by row index and column index
string val = ws.Rows[RowIndex].Columns[ColumnIndex].Value.ToString();
// Access a cell value by row index and column index
string val = ws.Rows[RowIndex].Columns[ColumnIndex].Value.ToString();
' Access a cell value by row index and column index
Dim val As String = ws.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:
// Get Cell Value Example
using IronXL;
class Program
{
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
int intValue = ws["C6"].Int32Value;
// Get value by Row and Column Address
string strValue = ws.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);
Console.ReadKey();
}
}
// Get Cell Value Example
using IronXL;
class Program
{
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
int intValue = ws["C6"].Int32Value;
// Get value by Row and Column Address
string strValue = ws.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);
Console.ReadKey();
}
}
' Get Cell Value Example
Imports IronXL
Friend Class Program
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 intValue As Integer = ws("C6").Int32Value
' Get value by Row and Column Address
Dim strValue As String = ws.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)
Console.ReadKey()
End Sub
End Class
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:
// Access data from a specific range
var rangeData = ws["From Cell Address : To Cell Address"];
// Access data from a specific range
var rangeData = ws["From Cell Address : To Cell Address"];
' Access data from a specific range
Dim rangeData = ws("From Cell Address : To Cell Address")
Let's see an example of how to use range to get data from an open Excel WorkSheet
:
// Get Data from Range Example
using IronXL;
class Program
{
static void Main(string[] args)
{
// Load Excel file
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 Example
using IronXL;
class Program
{
static void Main(string[] args)
{
// Load Excel file
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 Example
Imports IronXL
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Load Excel file
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
End Class
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:
// Access data from a specific row range
var rowData = ws["A1:E1"];
// 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.
// Get All Data Example
using IronXL;
class Program
{
static void Main(string[] args)
{
// Load Excel file
WorkBook wb = WorkBook.Load("sample2.xlsx");
WorkSheet ws = wb.GetWorkSheet("Sheet1");
// Access all rows of the open Excel WorkSheet
for (int i = 0; i < ws.Rows.Count(); i++)
{
// Access all columns of a specific row
for (int j = 0; j < ws.Columns.Count(); j++)
{
// Access each cell for the specified column
Console.WriteLine(ws.Rows[i].Columns[j].Value.ToString());
}
}
Console.ReadKey();
}
}
// Get All Data Example
using IronXL;
class Program
{
static void Main(string[] args)
{
// Load Excel file
WorkBook wb = WorkBook.Load("sample2.xlsx");
WorkSheet ws = wb.GetWorkSheet("Sheet1");
// Access all rows of the open Excel WorkSheet
for (int i = 0; i < ws.Rows.Count(); i++)
{
// Access all columns of a specific row
for (int j = 0; j < ws.Columns.Count(); j++)
{
// Access each cell for the specified column
Console.WriteLine(ws.Rows[i].Columns[j].Value.ToString());
}
}
Console.ReadKey();
}
}
' Get All Data Example
Imports IronXL
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Load Excel file
Dim wb As WorkBook = WorkBook.Load("sample2.xlsx")
Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
' Access all rows of the open Excel WorkSheet
For i As Integer = 0 To ws.Rows.Count() - 1
' Access all columns of a specific row
For j As Integer = 0 To ws.Columns.Count() - 1
' Access each cell for the specified column
Console.WriteLine(ws.Rows(i).Columns(j).Value.ToString())
Next j
Next i
Console.ReadKey()
End Sub
End Class
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 an Excel file in C#?
To open an Excel file in C#, install the IronXL library, use the WorkBook.Load() function to load the Excel file into a WorkBook object, and then open a specific WorkSheet using the WorkBook.GetWorkSheet() function.
What types of Excel files can I open with C#?
Using C#, you can open various Excel file types including .xls, .csv, .tsv, and .xlsx.
How do I access a specific cell value in an Excel WorkSheet using C#?
You can access a specific cell value by its address using ws["Cell Address"].ToString() or by specifying the row and column index using ws.Rows[RowIndex].Columns[ColumnIndex].Value.ToString().
How do I get data from a specific range in an Excel WorkSheet using C#?
To get data from a specific range, use the syntax ws["From Cell Address : To Cell Address"] to access the desired range of cells.
How can I read all data from an Excel WorkSheet in C#?
To read all data from an Excel WorkSheet, use loops to traverse each row and column, accessing each cell's value using ws.Rows[i].Columns[j].Value.ToString().
What is the purpose of the IronXL library in handling Excel files?
The IronXL library provides functions and classes to easily open, read, and manipulate Excel files within C# projects, simplifying tasks such as loading files, accessing data, and processing Excel content.
How do I install the IronXL library for my C# project?
You can install the IronXL library by adding the NuGet package IronXL.Excel to your project using the command `dotnet add package IronXL.Excel`.
Can I open an Excel WorkSheet by its index using C#?
Yes, you can open an Excel WorkSheet by its index using the syntax WorkSheet ws = wb.WorkSheets[index]; where 'index' is the position of the WorkSheet in the WorkBook.
Is it possible to work with Excel file ranges in C#?
Yes, IronXL allows you to work with Excel file ranges by specifying cell ranges such as "A1:E1" to access and manipulate data within that range.
Where can I find more information about IronXL functions?
You can refer to the IronXL API Reference resource for detailed information on functions, classes, namespaces, and more. Visit the API Reference at https://ironsoftware.com/csharp/excel/object-reference/api/.