IronXL How-Tos Manage Worksheet How to Manage Worksheets ByChaknith Bin April 6, 2023 Updated June 22, 2025 Share: 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. View the IronXL YouTube Playlist 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 Get started with IronXL Start using IronXL in your project today with a free trial. First Step: Start for Free 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 a new Excel workbook in the XLSX format WorkBook workBook = WorkBook.Create(ExcelFileFormat.XLSX); // Create multiple worksheets within the workbook // Each call to CreateWorkSheet initializes a new worksheet with the specified name WorkSheet workSheet1 = workBook.CreateWorkSheet("WorkSheet1"); // First worksheet named "WorkSheet1" WorkSheet workSheet2 = workBook.CreateWorkSheet("WorkSheet2"); // Second worksheet named "WorkSheet2" WorkSheet workSheet3 = workBook.CreateWorkSheet("WorkSheet3"); // Third worksheet named "WorkSheet3" WorkSheet workSheet4 = workBook.CreateWorkSheet("WorkSheet4"); // Fourth worksheet named "WorkSheet4" // Save the workbook with the worksheets to a file named "createNewWorkSheets.xlsx" workBook.SaveAs("createNewWorkSheets.xlsx"); Imports IronXL ' Create a new Excel workbook in the XLSX format Private workBook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX) ' Create multiple worksheets within the workbook ' Each call to CreateWorkSheet initializes a new worksheet with the specified name Private workSheet1 As WorkSheet = workBook.CreateWorkSheet("WorkSheet1") ' First worksheet named "WorkSheet1" Private workSheet2 As WorkSheet = workBook.CreateWorkSheet("WorkSheet2") ' Second worksheet named "WorkSheet2" Private workSheet3 As WorkSheet = workBook.CreateWorkSheet("WorkSheet3") ' Third worksheet named "WorkSheet3" Private workSheet4 As WorkSheet = workBook.CreateWorkSheet("WorkSheet4") ' Fourth worksheet named "WorkSheet4" ' Save the workbook with the worksheets to a file named "createNewWorkSheets.xlsx" workBook.SaveAs("createNewWorkSheets.xlsx") $vbLabelText $csharpLabel 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 // Import the IronXL library to work with Excel files using IronXL; // Load an existing workbook from a file named "createNewWorkSheets.xlsx" WorkBook workBook = WorkBook.Load("createNewWorkSheets.xlsx"); // Set the position of the worksheet named "workSheet2" to be the first sheet in the workbook // The sheet index is zero-based, so 0 refers to the first position try { // Method assumes that the workbook contains a sheet named "workSheet2" // and ensures "workSheet2" is placed at index 0 workBook.SetWorksheetPosition("workSheet2", 0); } catch(Exception e) { // Log or handle the error appropriately if the sheet is not found // or other exceptions arise Console.WriteLine($"An error occurred: {e.Message}"); } // Save the modified workbook to a new file named "setWorksheetPosition.xlsx" try { // Save changes to a new file preserving the original file workBook.SaveAs("setWorksheetPosition.xlsx"); } catch(Exception e) { // Log or handle any errors that occur during saving Console.WriteLine($"An error occurred while saving the file: {e.Message}"); } ' Import the IronXL library to work with Excel files Imports IronXL ' Load an existing workbook from a file named "createNewWorkSheets.xlsx" Private workBook As WorkBook = WorkBook.Load("createNewWorkSheets.xlsx") ' Set the position of the worksheet named "workSheet2" to be the first sheet in the workbook ' The sheet index is zero-based, so 0 refers to the first position Try ' Method assumes that the workbook contains a sheet named "workSheet2" ' and ensures "workSheet2" is placed at index 0 workBook.SetWorksheetPosition("workSheet2", 0) Catch e As Exception ' Log or handle the error appropriately if the sheet is not found ' or other exceptions arise Console.WriteLine($"An error occurred: {e.Message}") End Try ' Save the modified workbook to a new file named "setWorksheetPosition.xlsx" Try ' Save changes to a new file preserving the original file workBook.SaveAs("setWorksheetPosition.xlsx") Catch e As Exception ' Log or handle any errors that occur during saving Console.WriteLine($"An error occurred while saving the file: {e.Message}") End Try $vbLabelText $csharpLabel 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; // Load the Excel workbook from the specified file path WorkBook workBook = WorkBook.Load("createNewWorkSheets.xlsx"); // Check if the workbook has at least three worksheets if (workBook.WorkSheets.Count > 2) { // Set the third worksheet (zero-based index, so index 2 is the third sheet) as active workBook.SetActiveSheet(2); } else { // If there are less than three worksheets, log a message or handle the error Console.WriteLine("The workbook does not contain enough worksheets to set the third one as active."); } // Save the changes to a new Excel file workBook.SaveAs("setActiveTab.xlsx"); Imports IronXL ' Load the Excel workbook from the specified file path Private workBook As WorkBook = WorkBook.Load("createNewWorkSheets.xlsx") ' Check if the workbook has at least three worksheets If workBook.WorkSheets.Count > 2 Then ' Set the third worksheet (zero-based index, so index 2 is the third sheet) as active workBook.SetActiveSheet(2) Else ' If there are less than three worksheets, log a message or handle the error Console.WriteLine("The workbook does not contain enough worksheets to set the third one as active.") End If ' Save the changes to a new Excel file workBook.SaveAs("setActiveTab.xlsx") $vbLabelText $csharpLabel 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; // Load the Excel workbook from a file WorkBook workBook = WorkBook.Load("createNewWorkSheets.xlsx"); // Remove the first worksheet by index // Indices are zero-based, so 0 refers to the first sheet. workBook.RemoveWorkSheet(0); // Remove the worksheet named "workSheet2" // This method removes the worksheet by its name. workBook.RemoveWorkSheet("workSheet2"); // Save the edited workbook to a new file workBook.SaveAs("removeWorksheet.xlsx"); Imports IronXL ' Load the Excel workbook from a file Private workBook As WorkBook = WorkBook.Load("createNewWorkSheets.xlsx") ' Remove the first worksheet by index ' Indices are zero-based, so 0 refers to the first sheet. workBook.RemoveWorkSheet(0) ' Remove the worksheet named "workSheet2" ' This method removes the worksheet by its name. workBook.RemoveWorkSheet("workSheet2") ' Save the edited workbook to a new file workBook.SaveAs("removeWorksheet.xlsx") $vbLabelText $csharpLabel 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; // Create a new workbook with a specified Excel file format (XLSX in this case). WorkBook firstBook = WorkBook.Create(ExcelFileFormat.XLSX); // Create a second workbook with the default Excel file format. WorkBook secondBook = WorkBook.Create(); // Select the default/first worksheet in the first workbook. WorkSheet workSheet = firstBook.DefaultWorkSheet; // Duplicate the worksheet within the same workbook; the new sheet will be named "Copied Sheet". workSheet.CopySheet("Copied Sheet"); // Duplicate the worksheet to the second workbook and name it "Copied Sheet". workSheet.CopyTo(secondBook, "Copied Sheet"); // Save the first workbook to a file named "firstWorksheet.xlsx". firstBook.SaveAs("firstWorksheet.xlsx"); // Save the second workbook to a file named "secondWorksheet.xlsx". secondBook.SaveAs("secondWorksheet.xlsx"); Imports IronXL ' Create a new workbook with a specified Excel file format (XLSX in this case). Private firstBook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX) ' Create a second workbook with the default Excel file format. Private secondBook As WorkBook = WorkBook.Create() ' Select the default/first worksheet in the first workbook. Private workSheet As WorkSheet = firstBook.DefaultWorkSheet ' Duplicate the worksheet within the same workbook; the new sheet will be named "Copied Sheet". workSheet.CopySheet("Copied Sheet") ' Duplicate the worksheet to the second workbook and name it "Copied Sheet". workSheet.CopyTo(secondBook, "Copied Sheet") ' Save the first workbook to a file named "firstWorksheet.xlsx". firstBook.SaveAs("firstWorksheet.xlsx") ' Save the second workbook to a file named "secondWorksheet.xlsx". secondBook.SaveAs("secondWorksheet.xlsx") $vbLabelText $csharpLabel firstWorksheet.xlsx secondWorksheet.xlsx 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 Bin Chat with engineering team now 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. Ready to Get Started? Start Free Trial Total downloads: 1,446,926 View Licenses >