Add, Remove and Reorder Worksheets
The following sections describe how to add, remove, and reorder worksheets inside a workbook.
Add Worksheets
To add a new worksheet to a workbook, use its Worksheets collection. The collection exposes an Add() method that does not take arguments and returns the instance of the newly created worksheet. By default, worksheets are assigned the first available name in the sequence Sheet1, Sheet2, Sheet3, and so on. You can change the name of the worksheet through the Worksheet.Name property. For more information about renaming a worksheet, refer to Rename a Worksheet.
Example 1 creates a workbook from scratch and adds a single worksheet to it. Since this is the first worksheet in the workbook, it is also set as the active worksheet. All worksheets added after it do not become active.
Example 1: Create a Workbook and Add a Worksheet to It
Workbook workbook = new Workbook();
Worksheet newWorksheet = workbook.Worksheets.Add();
Remove Worksheets
The Worksheets collection of the workbook offers two methods for removing worksheets: Remove() and RemoveAt(). The Remove() method requires the worksheet name or the worksheet instance as an argument. The RemoveAt() method allows you to specify the index of the worksheet you want to remove.
Example 2 creates a workbook and adds four worksheets. All worksheets have their default names: Sheet1, Sheet2, Sheet3, and Sheet4. The code further demonstrates how to remove three worksheets using the remove methods listed previously.
Example 2: Add and Remove Worksheets
Workbook workbook = new Workbook();
workbook.Worksheets.Add(); // Sheet1
Worksheet secondWorksheet = workbook.Worksheets.Add(); // Sheet2
workbook.Worksheets.Add(); // Sheet3
workbook.Worksheets.Add(); // Sheet4
workbook.Worksheets.RemoveAt(3); // Removed Sheet4
workbook.Worksheets.Remove("Sheet1"); // Removed Sheet1
workbook.Worksheets.Remove(secondWorksheet); // Removed Sheet2
// the only worksheet left is Sheet3
Reorder Worksheets
To change the order in which worksheets appear inside the workbook, use the Move() method of the Sheets collection. The method allows you to move one or more consecutive sheets to a specified position. Example 3 shows how to insert four sheets and move the last one to the first position in the collection. When the workbook is visualized, the fourth sheet is the first one visible in the sheet tabs.
Example 3: Add and Reorder Worksheets
Workbook workbook = new Workbook();
workbook.Worksheets.Add(); // Sheet1
workbook.Worksheets.Add(); // Sheet2
workbook.Worksheets.Add(); // Sheet3
workbook.Worksheets.Add(); // Sheet4
workbook.Sheets.Move(3, 1, 0); // Move the fourth sheet to the first place