C# Open Excel Worksheets

This article was translated from English: Does it need improvement?
Translated
View the article in English

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.

Quickstart: Load a Workbook and Open a Worksheet in One Line

Just two simple method calls let you load any supported Excel file and open a named worksheet—no complex setup or interop required. It’s an easy get-started path using IronXL so you can start reading or editing data immediately.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronXL with NuGet Package Manager

    PM > Install-Package IronXL.Excel

  2. Copy and run this code snippet.

    WorkBook wb = WorkBook.Load("sample.xlsx"); WorkSheet ws = wb.GetWorkSheet("Sheet1");
  3. Deploy to test on your live environment

    Start using IronXL in your project today with a free trial
    arrow pointer

Minimal Workflow (5 Steps)

  • 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 Work related to C# Open Excel Worksheets

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
using IronXL;

// Get a worksheet by its name
WorkSheet workSheet = workBook.GetWorkSheet("SheetName");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

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")
$vbLabelText   $csharpLabel

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()
$vbLabelText   $csharpLabel

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:

: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()
$vbLabelText   $csharpLabel

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()
$vbLabelText   $csharpLabel

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)
$vbLabelText   $csharpLabel

This code displays the following output:

1output related to 4.1. Get Specific Cell Value

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

1excel related to 4.1. Get Specific Cell Value

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")
$vbLabelText   $csharpLabel

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);
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

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

2output related to 4.2. Get Data from Specific Range

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

2excel related to 4.2. Get Data from Specific Range

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
var rowData = workSheet["A1:E1"];
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

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
$vbLabelText   $csharpLabel

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


Tutorial Quick Access

Documentation related to 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

Questions Fréquemment Posées

Comment puis-je ouvrir et manipuler des feuilles de calcul Excel en utilisant C# ?

Vous pouvez ouvrir et manipuler des feuilles de calcul Excel en C# en utilisant la bibliothèque IronXL. Commencez par installer la bibliothèque, puis chargez votre fichier Excel dans un objet WorkBook en utilisant WorkBook.Load(). Vous pouvez sélectionner et ouvrir des feuilles de calcul spécifiques avec la méthode WorkBook.GetWorkSheet().

Quels types de fichiers Excel sont pris en charge en C# ?

En C#, vous pouvez travailler avec divers types de fichiers Excel tels que .xls, .csv, .tsv, et .xlsx en utilisant la bibliothèque IronXL.

Comment récupérer la valeur d'une cellule spécifique d'une feuille de calcul Excel en C# ?

Pour récupérer la valeur d'une cellule spécifique d'une feuille de calcul Excel en utilisant C#, utilisez la bibliothèque IronXL pour accéder aux cellules par leur adresse avec ws["Cell Address"].ToString() ou en spécifiant les indices de ligne et de colonne avec ws.Rows[RowIndex].Columns[ColumnIndex].Value.ToString().

Puis-je récupérer des données d'une plage de cellules définie dans une feuille de calcul en utilisant C# ?

Oui, vous pouvez récupérer des données d'une plage de cellules définie dans une feuille de calcul en utilisant C# en employant la syntaxe de la bibliothèque IronXL ws["From Cell Address : To Cell Address"] pour accéder aux données dans la plage spécifiée.

Comment puis-je lire toutes les données d'une feuille de calcul Excel de manière programmée ?

Pour lire toutes les données d'une feuille de calcul Excel de manière programmée en C#, vous pouvez parcourir chaque ligne et colonne en utilisant la bibliothèque IronXL, en accédant à la valeur de chaque cellule avec ws.Rows[i].Columns[j].Value.ToString().

Pourquoi les développeurs devraient-ils utiliser une bibliothèque pour gérer les fichiers Excel en C# ?

Utiliser une bibliothèque comme IronXL est bénéfique pour les développeurs car cela simplifie le processus d'ouverture, de lecture et de manipulation des fichiers Excel dans les projets C#, en fournissant des fonctions et des classes robustes pour gérer le contenu Excel efficacement.

Comment puis-je installer une bibliothèque de gestion des fichiers Excel dans mon projet C# ?

Pour installer la bibliothèque IronXL pour la gestion des fichiers Excel dans votre projet C#, utilisez le gestionnaire de packages NuGet avec la commande dotnet add package IronXL.Excel.

Est-il possible d'ouvrir une feuille de calcul par son index dans un classeur en utilisant C# ?

Oui, vous pouvez ouvrir une feuille de calcul par son index dans un classeur en utilisant la bibliothèque IronXL avec la syntaxe WorkSheet ws = wb.WorkSheets[index];, où 'index' représente la position de la feuille de calcul dans le classeur.

Comment puis-je travailler avec des plages de cellules spécifiques dans un fichier Excel en utilisant C# ?

IronXL vous permet de travailler avec des plages de cellules spécifiques en spécifiant des plages telles que "A1:E1" pour accéder et manipuler les données dans cette plage définie dans un fichier Excel.

Où puis-je trouver plus d'informations sur les fonctions disponibles pour la gestion des fichiers Excel en C# ?

Pour des informations détaillées sur les fonctions disponibles pour la gestion des fichiers Excel en C#, vous pouvez vous référer à la ressource de référence API IronXL, qui fournit une documentation complète sur les fonctions, les classes et les espaces de noms. Visitez la référence API à https://ironsoftware.com/csharp/excel/object-reference/api/.

Curtis Chau
Rédacteur technique

Curtis Chau détient un baccalauréat en informatique (Université de Carleton) et se spécialise dans le développement front-end avec expertise en Node.js, TypeScript, JavaScript et React. Passionné par la création d'interfaces utilisateur intuitives et esthétiquement plaisantes, Curtis aime travailler avec des frameworks modernes ...

Lire la suite
Prêt à commencer?
Nuget Téléchargements 1,686,155 | Version : 2025.11 vient de sortir