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 note
All 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

What is this library for managing Excel worksheets?

IronXL is a library for C# that simplifies the management of Excel worksheets without using Office Interop.

How can I create a new worksheet with this library?

You can create a new worksheet using the CreateWorksheet method in IronXL, which requires the worksheet name as a parameter.

How do I change the position of a worksheet?

Use the SetSheetPosition method in IronXL, providing the worksheet name and its new index position as parameters.

How can I set an active worksheet?

Set the active worksheet using the SetActiveTab method in IronXL with the index of the worksheet you want to set as active.

How do I remove a worksheet?

The RemoveWorksheet method in IronXL allows you to remove a worksheet by specifying its name or index position.

Can I copy a worksheet with this library?

Yes, you can copy a worksheet within the same workbook using the CopySheet method or to another workbook using the CopyTo method in IronXL.

Do I need Office Interop to manage worksheets with this library?

No, IronXL allows you to manage Excel worksheets without using Office Interop.

Where can I download this library?

You can download the IronXL library from the NuGet package manager at nuget.org.

Is it possible to merge cells in a newly created worksheet?

Yes, after creating a worksheet with IronXL, you can perform additional operations such as merging cells.

What file formats does this library support?

IronXL supports various Excel file formats, including XLSX.

Chaknith related to Copy Worksheet
Software Engineer
Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.