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.
How to Manage Excel Worksheets
- Download the C# library to manage Excel worksheets
- Create a new worksheet with a desired name using the
CreateWorksheet
method - Change the worksheet position to be more organized with the
SetSheetPosition
method - Set the active worksheet to eliminate distractions using the
SetActiveTab
method - Remove unused worksheets to reduce confusion by utilizing the
RemoveWorksheet
method
Install with NuGet
Install-Package IronXL.Excel
Download DLL
Manually install into your project
Install with NuGet
Install-Package IronXL.Excel
Download DLL
Manually install into your project
Start using IronPDF in your project today with a free trial.
Check out IronXL on Nuget for quick installation and deployment. With over 8 million downloads, it's transforming Excel with C#.
Install-Package IronXL.Excel
Consider installing the IronXL DLL directly. Download and manually install it for your project or GAC form: IronXL.zip
Manually install into your project
Download DLLManage 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
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")
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")
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")
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")
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
firstWorksheet.xlsx
secondWorksheet.xlsx