How to Manage Excel Worksheets in C#

In this video tutorial, you will learn how to manage Excel worksheets using the IronXL library in C#. The tutorial begins by guiding viewers to set up a Visual Studio C# project with IronXL installed. Key tasks demonstrated include creating a new Excel workbook and populating it with four worksheets using the 'create worksheet' method. IronXL's versatility is showcased by shuffling worksheet order, setting an active tab with 'set sheet position,' and removing worksheets. Additionally, viewers learn to clone and rename sheets, saving changes with a different file name for comparison. The tutorial emphasizes the ease of managing Excel files with IronXL, encouraging users to explore further. The video concludes with a call to subscribe for more tips and tricks, and a link is provided to try IronXL software.

Sample Code:

// Necessary using directive to work with IronXL
using IronXL;

class ExcelManagement
{
    static void Main()
    {
        // Create a new workbook
        WorkBook workbook = WorkBook.Create(ExcelFileFormat.XLSX);

        // Add four worksheets to the workbook
        workbook.CreateWorkSheet("Sheet1");
        workbook.CreateWorkSheet("Sheet2");
        workbook.CreateWorkSheet("Sheet3");
        workbook.CreateWorkSheet("Sheet4");

        // Shuffle the order of worksheets
        workbook.WorkSheets.Move("Sheet4", 0); // Move Sheet4 to the first position

        // Set an active worksheet
        workbook.WorkSheets[0].SetActive();

        // Remove a worksheet by name
        workbook.WorkSheets.Remove("Sheet2");

        // Clone and rename a worksheet
        var clonedSheet = workbook.WorkSheets["Sheet1"].Clone("Sheet1_Clone");

        // Save changes with a different file name
        workbook.SaveAs("ManagedExcelFile.xlsx");

        // Inform the user about the successful operations
        Console.WriteLine("Excel workbook managed successfully.");
    }
}
// Necessary using directive to work with IronXL
using IronXL;

class ExcelManagement
{
    static void Main()
    {
        // Create a new workbook
        WorkBook workbook = WorkBook.Create(ExcelFileFormat.XLSX);

        // Add four worksheets to the workbook
        workbook.CreateWorkSheet("Sheet1");
        workbook.CreateWorkSheet("Sheet2");
        workbook.CreateWorkSheet("Sheet3");
        workbook.CreateWorkSheet("Sheet4");

        // Shuffle the order of worksheets
        workbook.WorkSheets.Move("Sheet4", 0); // Move Sheet4 to the first position

        // Set an active worksheet
        workbook.WorkSheets[0].SetActive();

        // Remove a worksheet by name
        workbook.WorkSheets.Remove("Sheet2");

        // Clone and rename a worksheet
        var clonedSheet = workbook.WorkSheets["Sheet1"].Clone("Sheet1_Clone");

        // Save changes with a different file name
        workbook.SaveAs("ManagedExcelFile.xlsx");

        // Inform the user about the successful operations
        Console.WriteLine("Excel workbook managed successfully.");
    }
}
' Necessary using directive to work with IronXL
Imports IronXL

Friend Class ExcelManagement
	Shared Sub Main()
		' Create a new workbook
		Dim workbook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)

		' Add four worksheets to the workbook
		workbook.CreateWorkSheet("Sheet1")
		workbook.CreateWorkSheet("Sheet2")
		workbook.CreateWorkSheet("Sheet3")
		workbook.CreateWorkSheet("Sheet4")

		' Shuffle the order of worksheets
		workbook.WorkSheets.Move("Sheet4", 0) ' Move Sheet4 to the first position

		' Set an active worksheet
		workbook.WorkSheets(0).SetActive()

		' Remove a worksheet by name
		workbook.WorkSheets.Remove("Sheet2")

		' Clone and rename a worksheet
		Dim clonedSheet = workbook.WorkSheets("Sheet1").Clone("Sheet1_Clone")

		' Save changes with a different file name
		workbook.SaveAs("ManagedExcelFile.xlsx")

		' Inform the user about the successful operations
		Console.WriteLine("Excel workbook managed successfully.")
	End Sub
End Class
$vbLabelText   $csharpLabel

Further Reading: How to Manage Worksheets

Regan Pun
Software Engineer
Regan graduated from the University of Reading, with a BA in Electronic Engineering. Before joining Iron Software, his previous job roles had him laser-focused on single tasks; and what he most enjoys at Iron Software is the spectrum of work he gets to undertake, whether it’s adding value to sales, technical support, product development or marketing. He enjoys understanding the way developers are using the Iron Software library, and using that knowledge to continually improve documentation and develop the products.
NEXT >
How to Read Excel Files in C# using IronXL