Comment gérer les feuilles Excel en C#

How to Manage Worksheets

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

The IronXL library simplifies the management of worksheets using C# code. With IronXL, you can perform actions such as creating and deleting worksheets, changing the position of worksheets, and setting the active worksheet in an Excel file, all without the need for Office Interop.

Quickstart: Add a New Worksheet Instantly

This example shows how effortlessly you can create a new worksheet using IronXL in just one line—no boilerplate, no Interop—so you can get straight to managing your Excel workbook in C#.

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.

    IronXL.WorkBook wb = IronXL.WorkBook.Create(ExcelFileFormat.XLSX).CreateWorkSheet("NewSheet");
  3. Deploy to test on your live environment

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

Manage Worksheet Example

Managing worksheets requires the ability to create, move, and delete worksheets. IronXL allows you to accomplish each of these actions with just a single line of code.

Veuillez noterAll the index positions mentioned below follow zero-based indexing

Create Worksheet

The CreateWorksheet method enables the creation of a new worksheet. It requires the worksheet name as the only parameter. This method also returns the created worksheet object, allowing you to perform additional operations such as merging cells right after creating it.

:path=/static-assets/excel/content-code-examples/how-to/manage-worksheet-create-worksheet.cs
using IronXL;

// Create new Excel spreadsheet
WorkBook workBook = WorkBook.Create(ExcelFileFormat.XLSX);

// Create worksheets
WorkSheet workSheet1 = workBook.CreateWorkSheet("workSheet1");
WorkSheet workSheet2 = workBook.CreateWorkSheet("workSheet2");
WorkSheet workSheet3 = workBook.CreateWorkSheet("workSheet3");
WorkSheet workSheet4 = workBook.CreateWorkSheet("workSheet4");


workBook.SaveAs("createNewWorkSheets.xlsx");
Imports IronXL

' Create new Excel spreadsheet
Private workBook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)

' Create worksheets
Private workSheet1 As WorkSheet = workBook.CreateWorkSheet("workSheet1")
Private workSheet2 As WorkSheet = workBook.CreateWorkSheet("workSheet2")
Private workSheet3 As WorkSheet = workBook.CreateWorkSheet("workSheet3")
Private workSheet4 As WorkSheet = workBook.CreateWorkSheet("workSheet4")


workBook.SaveAs("createNewWorkSheets.xlsx")
$vbLabelText   $csharpLabel
Create Worksheets

Set Worksheet Position

The SetSheetPosition method allows you to change or move the position of a worksheet. It requires two parameters: the worksheet name as a String and its index position as an Integer.

:path=/static-assets/excel/content-code-examples/how-to/manage-worksheet-set-sheet-position.cs
using IronXL;

WorkBook workBook = WorkBook.Load("createNewWorkSheets.xlsx");

// Set worksheet position
workBook.SetSheetPosition("workSheet2", 0);

workBook.SaveAs("setWorksheetPosition.xlsx");
Imports IronXL

Private workBook As WorkBook = WorkBook.Load("createNewWorkSheets.xlsx")

' Set worksheet position
workBook.SetSheetPosition("workSheet2", 0)

workBook.SaveAs("setWorksheetPosition.xlsx")
$vbLabelText   $csharpLabel
Change Worksheet Position

Set Active Worksheet

Setting the active worksheet means specifying which worksheet should be opened by default when the workbook is first opened in other data visualization tools like Excel. To achieve this, use the SetActiveTab method with the index position of the worksheet.

:path=/static-assets/excel/content-code-examples/how-to/manage-worksheet-set-active-tab.cs
using IronXL;

WorkBook workBook = WorkBook.Load("createNewWorkSheets.xlsx");

// Set active for workSheet3
workBook.SetActiveTab(2);

workBook.SaveAs("setActiveTab.xlsx");
Imports IronXL

Private workBook As WorkBook = WorkBook.Load("createNewWorkSheets.xlsx")

' Set active for workSheet3
workBook.SetActiveTab(2)

workBook.SaveAs("setActiveTab.xlsx")
$vbLabelText   $csharpLabel
Set Active Worksheet

Remove Worksheet

Removing a worksheet can also be done using IronXL. Utilize the RemoveWorksheet method along with the index position of the worksheet. If the position of the worksheet is unknown, you can also use the name of the worksheet to delete it.

:path=/static-assets/excel/content-code-examples/how-to/manage-worksheet-remove-worksheet.cs
using IronXL;

WorkBook workBook = WorkBook.Load("createNewWorkSheets.xlsx");

// Remove workSheet1
workBook.RemoveWorkSheet(1);

// Remove workSheet2
workBook.RemoveWorkSheet("workSheet2");

workBook.SaveAs("removeWorksheet.xlsx");
Imports IronXL

Private workBook As WorkBook = WorkBook.Load("createNewWorkSheets.xlsx")

' Remove workSheet1
workBook.RemoveWorkSheet(1)

' Remove workSheet2
workBook.RemoveWorkSheet("workSheet2")

workBook.SaveAs("removeWorksheet.xlsx")
$vbLabelText   $csharpLabel
Remove Worksheet

Copy Worksheet

A worksheet can be copied within the same workbook or across different workbooks. To duplicate a worksheet within the same workbook, use the CopySheet method. To copy a worksheet to a different workbook, use the CopyTo method.

:path=/static-assets/excel/content-code-examples/how-to/manage-worksheet-copy-worksheet.cs
using IronXL;

WorkBook firstBook = WorkBook.Create(ExcelFileFormat.XLSX);
WorkBook secondBook = WorkBook.Create();

// Select first worksheet in the workbook
WorkSheet workSheet = firstBook.DefaultWorkSheet;

// Duplicate the worksheet to the same workbook
workSheet.CopySheet("Copied Sheet");

// Duplicate the worksheet to another workbook with the specified name
workSheet.CopyTo(secondBook, "Copied Sheet");

firstBook.SaveAs("firstWorksheet.xlsx");
secondBook.SaveAs("secondWorksheet.xlsx");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel
First Worksheet
Second Worksheet

Questions Fréquemment Posées

Comment puis-je créer une nouvelle feuille de calcul en C# ?

Vous pouvez utiliser la méthode CreateWorksheet dans IronXL pour ajouter une nouvelle feuille de calcul à votre classeur. Il suffit de spécifier le nom de la feuille de calcul en tant que paramètre.

Quel est l'avantage d'utiliser IronXL par rapport à Office Interop pour gérer les feuilles de calcul Excel ?

IronXL vous permet de gérer les feuilles de calcul Excel sans avoir besoin de Microsoft Office Interop, simplifiant le processus et réduisant les dépendances aux installations Office.

Comment changer l'ordre des feuilles de calcul dans un classeur ?

Pour changer l'ordre des feuilles de calcul dans un classeur, utilisez la méthode SetSheetPosition dans IronXL, où vous pouvez spécifier le nom de la feuille de calcul et sa nouvelle position d'index.

Comment puis-je définir une feuille de calcul spécifique comme l'onglet actif dans Excel ?

Utilisez la méthode SetActiveTab dans IronXL pour définir une feuille de calcul spécifique comme l'onglet actif en fournissant l'index de la feuille que vous souhaitez activer.

Quelle est la méthode pour supprimer une feuille de calcul dans IronXL ?

Vous pouvez supprimer une feuille de calcul en utilisant la méthode RemoveWorksheet dans IronXL, en spécifiant soit son nom, soit sa position d'index.

Comment puis-je copier une feuille de calcul dans un autre classeur ?

Pour copier une feuille de calcul dans un autre classeur, utilisez la méthode CopyTo disponible dans IronXL. Cette méthode vous permet de dupliquer la feuille de calcul dans un classeur différent de manière efficace.

Est-il possible de copier une feuille de calcul dans le même classeur en utilisant IronXL ?

Oui, vous pouvez copier une feuille de calcul dans le même classeur en utilisant la méthode CopySheet dans IronXL.

IronXL peut-il fusionner des cellules dans une feuille de calcul ?

Oui, IronXL fournit des fonctionnalités pour fusionner des cellules dans une feuille de calcul après sa création, permettant un formatage plus personnalisé.

Quels formats de fichiers Excel sont supportés par IronXL ?

IronXL prend en charge divers formats de fichiers Excel, y compris XLSX, qui est largement utilisé pour les fichiers Excel modernes.

Où puis-je trouver la bibliothèque IronXL pour téléchargement ?

Vous pouvez télécharger la bibliothèque IronXL depuis le gestionnaire de paquets NuGet sur nuget.org pour l'intégrer dans vos projets C#.

Chaknith Bin
Ingénieur logiciel
Chaknith travaille sur IronXL et IronBarcode. Il a une expertise approfondie en C# et .NET, aidant à améliorer le logiciel et à soutenir les clients. Ses idées issues des interactions avec les utilisateurs contribuent à de meilleurs produits, documentation et expérience globale.
Prêt à commencer?
Nuget Téléchargements 1,686,155 | Version : 2025.11 vient de sortir