Back to Devexpress

How to: Add a New Worksheet

windowsforms-15361-controls-and-libraries-spreadsheet-examples-worksheets-how-to-add-a-new-worksheet.md

latest3.9 KB
Original Source

How to: Add a New Worksheet

  • Dec 09, 2022
  • 2 minutes to read

This example demonstrates how to add a new worksheet to a workbook. To do this, use the WorksheetCollection.Add method of the WorksheetCollection collection accessed via IWorkbook.Worksheets.

To create a new worksheet and specify where it should be inserted, call the WorksheetCollection.Insert method with the zero-based worksheet index passed as a parameter.

To specify a worksheet name, use the Worksheet.Name property, or pass the worksheet name to the WorksheetCollection.Add or WorksheetCollection.Insert method as a parameter. When naming a worksheet, take into account the constraints listed in the How to: Rename a Worksheet document.

View Example

csharp
// Add a new worksheet to the workbook. 
// The worksheet will be inserted into the end of the existing collection of worksheets.
// Worksheet name is "SheetN", where N is a number following the largest number used in existing worksheet names of the same type.
workbook.Worksheets.Add();

// Add a new worksheet under the specified name.
workbook.Worksheets.Add().Name = "TestSheet1";

workbook.Worksheets.Add("TestSheet2");

// Add a new worksheet at the specified position in the collection of worksheets.
workbook.Worksheets.Insert(1, "TestSheet3");

workbook.Worksheets.Insert(3);
vb
' Add a new worksheet to the workbook. 
' The worksheet will be inserted into the end of the existing collection of worksheets.
' Worksheet name is "SheetN", where N is a number following the largest number used in existing worksheet names of the same type.
workbook.Worksheets.Add()

' Add a new worksheet under the specified name.
workbook.Worksheets.Add().Name = "TestSheet1"

workbook.Worksheets.Add("TestSheet2")

' Add a new worksheet at the specified position in the collection of worksheets.
workbook.Worksheets.Insert(1, "TestSheet3")

workbook.Worksheets.Insert(3)

The image below shows how new worksheets are inserted in a workbook.

The SpreadsheetControl.SheetInserted event is raised after a new worksheet has been created via the control’s UI.

See Also

How to: Rename a Worksheet

How to: Move a Worksheet to Another Location in a Workbook

How to: Copy Worksheets

How to: Delete a Worksheet

Worksheets in Spreadsheet Documents