How to Manage Worksheets

by Chaknith Bin

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")
VB   C#
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")
VB   C#
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")
VB   C#
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")
VB   C#
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
VB   C#
First Worksheet
Second Worksheet
Chaknith related to Copy Worksheet

Chaknith Bin

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.