How to Manage Worksheets

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.

Get started with IronXL

Start using IronXL in your project today with a free trial.

First Step:
green 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.

Please noteAll 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

Frequently Asked Questions

How can I create a new worksheet in C#?

You can use the CreateWorksheet method in IronXL to add a new worksheet to your workbook. Simply specify the worksheet name as a parameter.

What is the advantage of using IronXL over Office Interop for managing Excel worksheets?

IronXL allows you to manage Excel worksheets without the need for Microsoft Office Interop, simplifying the process and reducing dependencies on Office installations.

How do I change the order of worksheets in a workbook?

To change the order of worksheets in a workbook, utilize the SetSheetPosition method in IronXL, where you can specify the worksheet name and its new index position.

How can I set a specific worksheet as the active tab in Excel?

Use the SetActiveTab method in IronXL to set a specific worksheet as the active tab by providing the index of the worksheet you want to activate.

What is the method to remove a worksheet in IronXL?

You can remove a worksheet by using the RemoveWorksheet method in IronXL, specifying either its name or index position.

How can I copy a worksheet to another workbook?

To copy a worksheet to another workbook, use the CopyTo method available in IronXL. This method allows you to duplicate the worksheet into a different workbook efficiently.

Is it possible to copy a worksheet within the same workbook using IronXL?

Yes, you can copy a worksheet within the same workbook by using the CopySheet method in IronXL.

Can IronXL merge cells in a worksheet?

Yes, IronXL provides functionalities to merge cells in a worksheet after its creation, allowing for more customized formatting.

Which Excel file formats are supported by IronXL?

IronXL supports various Excel file formats, including XLSX, which is widely used for modern Excel files.

Where can I find the IronXL library for download?

You can download the IronXL library from the NuGet package manager at nuget.org to integrate it into your C# projects.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.