Back to Devexpress

How to: Access a Worksheet

officefileapi-12078-spreadsheet-document-api-examples-worksheets-how-to-access-a-worksheet.md

latest2.2 KB
Original Source

How to: Access a Worksheet

  • Sep 19, 2023
  • 2 minutes to read

Important

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

This example demonstrates how to access worksheets in a workbook. Use the Workbook.Worksheets property to get a collection of worksheets contained in a workbook (the WorksheetCollection object).

To get an individual worksheet by its index or name, use the WorksheetCollection.Item property.

csharp
using DevExpress.Spreadsheet;
// ...

Workbook workbook = new Workbook();

// Access a collection of worksheets.
WorksheetCollection worksheets = workbook.Worksheets;

// Access a worksheet by its index.
Worksheet worksheet1 = workbook.Worksheets[0];

// Access a worksheet by its name.
Worksheet worksheet2 = workbook.Worksheets["Sheet2"];
vb
Imports DevExpress.Spreadsheet
' ...

Dim workbook As New Workbook()

' Access a collection of worksheets.
Dim worksheets As WorksheetCollection = workbook.Worksheets

' Access a worksheet by its index.
Dim worksheet1 As Worksheet = workbook.Worksheets(0)

' Access a worksheet by its name.
Dim worksheet2 As Worksheet = workbook.Worksheets("Sheet2")

A worksheet index is zero-based. It specifies the worksheet position within a collection. The worksheet name is unique within the collection, and is shown on a worksheet tab. The image below shows worksheet indexes and names in a workbook that is opened in Microsoft® Excel®.

See Also

How to: Move a Worksheet to another Location

How to: Rename a Worksheet