officefileapi-devexpress-dot-spreadsheet-0e97ede8.md
A collection of worksheets in a workbook.
Namespace : DevExpress.Spreadsheet
Assembly : DevExpress.Spreadsheet.v25.2.Core.dll
NuGet Package : DevExpress.Spreadsheet.Core
public interface WorksheetCollection :
ISimpleCollection<Worksheet>,
IEnumerable<Worksheet>,
IEnumerable,
ICollection
Public Interface WorksheetCollection
Inherits ISimpleCollection(Of Worksheet),
IEnumerable(Of Worksheet),
IEnumerable,
ICollection
The following members return WorksheetCollection objects:
Use the WorksheetCollection object’s members to access, add, and remove worksheets.
Use the WorksheetCollection.Item property to access a worksheet in a workbook.
using DevExpress.Spreadsheet;
// ...
// Access the worksheet collection.
WorksheetCollection worksheets = workbook.Worksheets;
// Access the first worksheet.
Worksheet worksheet1 = workbook.Worksheets[0];
Imports DevExpress.Spreadsheet
' ...
' Access the worksheet collection.
Dim worksheets As WorksheetCollection = workbook.Worksheets
' Access the first worksheet.
Dim worksheet1 As Worksheet = workbook.Worksheets(0)
A worksheet index is zero-based. It specifies the worksheet position within a collection.
using DevExpress.Spreadsheet;
// ...
// Access the worksheet collection.
WorksheetCollection worksheets = workbook.Worksheets;
// Access the worksheet with the specified name.
Worksheet worksheet2 = workbook.Worksheets["MainSheet"];
Imports DevExpress.Spreadsheet
' ...
' Access the worksheet collection.
Dim worksheets As WorksheetCollection = workbook.Worksheets
' Access the worksheet with the specified name.
Dim worksheet2 As Worksheet = workbook.Worksheets("MainSheet")
A worksheet name is unique within the collection and is shown on a worksheet tab.
Use the WorksheetCollection.ActiveWorksheet property to specify the active worksheet in a workbook.
using DevExpress.Spreadsheet;
// ...
// Set "Sheet2" as the active worksheet.
workbook.Worksheets.ActiveWorksheet = workbook.Worksheets["Sheet2"];
Imports DevExpress.Spreadsheet
' ...
' Set "Sheet2" as the active worksheet.
workbook.Worksheets.ActiveWorksheet = workbook.Worksheets("Sheet2")
Use the WorksheetCollection.Add method to add a worksheet to the end of the worksheet collection.
using DevExpress.Spreadsheet;
// ...
// Add a worksheet with the default name.
// Default names are "Sheet1", "Sheet2", ..., "SheetN".
workbook.Worksheets.Add();
// Add new worksheets with the specified names.
workbook.Worksheets.Add().Name = "TestSheet1";
workbook.Worksheets.Add("TestSheet2");
Imports DevExpress.Spreadsheet
' ...
' Add a worksheet with the default name.
' Default names are "Sheet1", "Sheet2", ..., "SheetN".
workbook.Worksheets.Add()
' Add new worksheets with the specified names.
workbook.Worksheets.Add().Name = "TestSheet1"
workbook.Worksheets.Add("TestSheet2")
The WorksheetCollection.Insert method allows you to insert a worksheet at the specified position in the collection.
using DevExpress.Spreadsheet;
// ...
// Insert a worksheet at the second position in the workbook.
workbook.Worksheets.Insert(1, "TestSheet3");
// Insert a worksheet with the default name at the fourth position in the workbook.
workbook.Worksheets.Insert(3);
Imports DevExpress.Spreadsheet
' ...
' Insert a worksheet at the second position in the workbook.
workbook.Worksheets.Insert(1, "TestSheet3")
' Insert a worksheet with the default name at the fourth position in the workbook.
workbook.Worksheets.Insert(3)
Use the following methods to delete a worksheet:
WorksheetCollection.Remove - removes a specific worksheet from the collection.
WorksheetCollection.RemoveAt - removes the worksheet with the specified index from the collection.
Note
A workbook must contain at least one visible worksheet.
using DevExpress.Spreadsheet;
// ...
// Delete the first worksheet from the workbook.
workbook.Worksheets.RemoveAt(0);
// Delete the "Sheet2" worksheet from the workbook.
workbook.Worksheets.Remove(workbook.Worksheets["Sheet2"]);
Imports DevExpress.Spreadsheet
' ...
' Delete the first worksheet from the workbook.
workbook.Worksheets.RemoveAt(0)
' Delete the "Sheet2" worksheet from the workbook.
workbook.Worksheets.Remove(workbook.Worksheets("Sheet2"))
See Also