How to Manage Worksheet
IronXL library makes managing worksheet using C# code as easy as possible. The actions of create & delete worksheet, change worksheets position, and set active worksheet in Excel file can be achieved without using Office Interop.
How to Manage Excel Worksheet

- Download C# library to manage excel worksheet
- Create new worksheet with desired name using
CreateWorkSheet
method - Change worksheet position to be more organize with
SetSheetPosition
method - Set active worksheet to eliminate distraction with
SetActiveTab
method - Remove unused worksheet to reduce confusion utilizing
RemoveWorkSheet
method

Install with NuGet
Install-Package IronXL.Excel
Manage Worksheet Example
Managing Worksheet requires the ability to create, move, and delete worksheet. IronXL can achieve each of these actions with a single line of code.
Please note
- All the index position mentioned below follows zero-based indexing.
Create Worksheet
The CreateWorkSheet
method allows creating worksheet possible. It requires the worksheet name as the only parameter. This method will also return the created worksheet object back, therefore more process such as merge cells can be done right away 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")

Set Worksheet Position
SetSheetPosition
method can be used to change or move worksheet position. The two parameters are required. The worksheet name as String and its index position as 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")

Set Active Worksheet
Set active worksheet means to set which worksheet to be opened by default when the workbook is first opened by other data visualization tool like Excel. To achieve this use 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")

Remove Worksheet
Removing the worksheet can also be done with IronXL. Use RemoveWorkSheet
method along with index position of the worksheet. In the case of worksheet's position is unknown, the name of the worksheet can also be used to delete the worksheet.
: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")
