Back to Devexpress

Workbook.Worksheets Property

officefileapi-devexpress-dot-spreadsheet-dot-workbook-67474a47.md

latest12.1 KB
Original Source

Workbook.Worksheets Property

Returns the collection of worksheets in a workbook.

You need a license for the DevExpress Office File API Subscription or DevExpress Universal Subscription to use this property in production code.

Namespace : DevExpress.Spreadsheet

Assembly : DevExpress.Docs.v25.2.dll

NuGet Package : DevExpress.Document.Processor

Declaration

csharp
public WorksheetCollection Worksheets { get; }
vb
Public ReadOnly Property Worksheets As WorksheetCollection

Property Value

TypeDescription
WorksheetCollection

The collection of Worksheet objects.

|

Remarks

Use the WorksheetCollection object’s members to access, add, and remove worksheets.

Access a Worksheet

Use the WorksheetCollection.Item property to access a worksheet in a workbook.

Obtain the worksheet at the specified index

csharp
using DevExpress.Spreadsheet;
// ...

Workbook workbook = new Workbook();

// Access the worksheet collection.
WorksheetCollection worksheets = workbook.Worksheets;

// Access the first worksheet.
Worksheet worksheet1 = workbook.Worksheets[0];
vb
Imports DevExpress.Spreadsheet
' ...

Dim workbook As New Workbook()

' 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.

Obtain the worksheet with a given name

csharp
using DevExpress.Spreadsheet;
// ...

Workbook workbook = new Workbook();

// Access the worksheet collection.
WorksheetCollection worksheets = workbook.Worksheets;

// Access the worksheet with the specified name.
Worksheet worksheet2 = workbook.Worksheets["MainSheet"];
vb
Imports DevExpress.Spreadsheet
' ...

Dim workbook As New Workbook()

' 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.

Set an Active Worksheet

Use the WorksheetCollection.ActiveWorksheet property to specify the active worksheet in a workbook.

csharp
using DevExpress.Spreadsheet;
// ...

// Set "Sheet2" as the active worksheet.
workbook.Worksheets.ActiveWorksheet = workbook.Worksheets["Sheet2"];
vb
Imports DevExpress.Spreadsheet
' ...

' Set "Sheet2" as the active worksheet.
workbook.Worksheets.ActiveWorksheet = workbook.Worksheets("Sheet2")

Add a New Worksheet

Use the WorksheetCollection.Add method to add a worksheet to the end of the worksheet collection.

csharp
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");
vb
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.

csharp
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);
vb
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)

Delete a Worksheet

Use the following methods to delete a worksheet:

Note

A workbook must contain at least one visible worksheet.

csharp
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"]);
vb
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"))

The following code snippets (auto-collected from DevExpress Examples) contain references to the Worksheets property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

spreadsheet-document-api-chart-examples/CS/SpreadsheetDocServerChartAPISamples/CodeExamples/ProtectionActions.cs#L14

csharp
#region #ProtectChart
Worksheet worksheet = workbook.Worksheets["chartTask3"];
workbook.Worksheets.ActiveWorksheet = worksheet;

spreadsheet-document-api-examples-part2/CS/SpreadsheetDocServerAPIPart2/CodeExamples/ExportActions.cs#L14

csharp
#region #ExportToHTML
Worksheet worksheet = workbook.Worksheets["Grouping"];
workbook.Worksheets.ActiveWorksheet = worksheet;

spreadsheet-document-api-examples-part1/CS/SpreadsheetExamples/SpreadsheetActions/RowAndColumnActions.cs#L20

csharp
static void InsertRowsColumns(Workbook workbook) {
    Worksheet worksheet = workbook.Worksheets[0];

spreadsheet-document-api-import-data-from-data-sources/CS/DataImportExample/Form1.cs#L25

csharp
{
    Worksheet worksheet = workbook.Worksheets[0];
    worksheet.Clear(worksheet.GetUsedRange());

spreadsheet-document-api-data-binding/CS/SpreadsheetApiDataBinding/Program.cs#L14

csharp
// Bind a first worksheet to a BindingList
BindWeatherReportToRange(MyWeatherReportSource.DataAsBindingList, workbook.Worksheets[0]);

spreadsheet-document-api-chart-examples/VB/SpreadsheetDocServerChartAPISamples/CodeExamples/LegendActions.vb#L17

vb
#Region "#HideLegend"
            Dim worksheet As Worksheet = workbook.Worksheets("chartTask3")
            workbook.Worksheets.ActiveWorksheet = worksheet

spreadsheet-document-api-examples-part2/VB/SpreadsheetDocServerAPIPart2/CodeExamples/ExportActions.vb#L11

vb
' #Region "#ExportToHTML"
            Dim worksheet As Worksheet = workbook.Worksheets("Grouping")
            workbook.Worksheets.ActiveWorksheet = worksheet

spreadsheet-document-api-examples-part1/VB/SpreadsheetExamples/SpreadsheetActions/RowAndColumnActions.vb#L19

vb
Private Sub InsertRowsColumns(ByVal workbook As Workbook)
    Dim worksheet As Worksheet = workbook.Worksheets(0)
    ' Populate cells with data.

spreadsheet-document-api-import-data-from-data-sources/VB/DataImportExample/Form1.vb#L19

vb
Try
    Dim worksheet As Worksheet = workbook.Worksheets(0)
    worksheet.Clear(worksheet.GetUsedRange())

spreadsheet-document-api-data-binding/VB/SpreadsheetApiDataBinding/Program.vb#L13

vb
' Bind a first worksheet to a BindingList
BindWeatherReportToRange(MyWeatherReportSource.DataAsBindingList, workbook.Worksheets(0))

Implements

Worksheets

See Also

Worksheets in Spreadsheet Documents

Worksheet Examples

Workbook Class

Workbook Members

DevExpress.Spreadsheet Namespace