Cómo gestionar hojas de trabajo de 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.

Por favor notaAll 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

Preguntas Frecuentes

¿Cómo puedo crear una nueva hoja de trabajo en C#?

Puedes usar el método CreateWorksheet en IronXL para agregar una nueva hoja de trabajo a tu libro de trabajo. Simplemente especifica el nombre de la hoja de trabajo como un parámetro.

¿Cuál es la ventaja de usar IronXL sobre Office Interop para administrar hojas de trabajo de Excel?

IronXL te permite administrar hojas de trabajo de Excel sin la necesidad de Microsoft Office Interop, simplificando el proceso y reduciendo las dependencias de instalaciones de Office.

¿Cómo cambio el orden de las hojas de trabajo en un libro?

Para cambiar el orden de las hojas de trabajo en un libro, utiliza el método SetSheetPosition en IronXL, donde puedes especificar el nombre de la hoja de trabajo y su nueva posición de índice.

¿Cómo puedo establecer una hoja de trabajo específica como la pestaña activa en Excel?

Usa el método SetActiveTab en IronXL para establecer una hoja de trabajo específica como la pestaña activa al proporcionar el índice de la hoja de trabajo que deseas activar.

¿Cuál es el método para eliminar una hoja de trabajo en IronXL?

Puedes eliminar una hoja de trabajo usando el método RemoveWorksheet en IronXL, especificando ya sea su nombre o posición de índice.

¿Cómo puedo copiar una hoja de trabajo a otro libro de trabajo?

Para copiar una hoja de trabajo a otro libro de trabajo, utiliza el método CopyTo disponible en IronXL. Este método te permite duplicar la hoja de trabajo en un libro de trabajo diferente de manera eficiente.

¿Es posible copiar una hoja de trabajo dentro del mismo libro de trabajo usando IronXL?

Sí, puedes copiar una hoja de trabajo dentro del mismo libro de trabajo usando el método CopySheet en IronXL.

¿Puede IronXL fusionar celdas en una hoja de trabajo?

Sí, IronXL proporciona funcionalidades para fusionar celdas en una hoja de trabajo después de su creación, permitiendo una personalización más detallada del formato.

¿Qué formatos de archivo Excel son soportados por IronXL?

IronXL soporta varios formatos de archivo Excel, incluyendo XLSX, que es ampliamente utilizado para archivos modernos de Excel.

¿Dónde puedo encontrar la biblioteca IronXL para descargar?

Puedes descargar la biblioteca IronXL desde el gestor de paquetes NuGet en nuget.org para integrarla en tus proyectos C#.

Chaknith Bin
Ingeniero de Software
Chaknith trabaja en IronXL e IronBarcode. Tiene un profundo conocimiento en C# y .NET, ayudando a mejorar el software y apoyar a los clientes. Sus conocimientos derivados de las interacciones con los usuarios contribuyen a mejores productos, documentación y experiencia en general.
¿Listo para empezar?
Nuget Descargas 1,686,155 | Versión: 2025.11 recién lanzado