Manage Worksheets
- 6 minutes to read
The Spreadsheet Document API allows you to add, rename, hide, and remove worksheets in a workbook.
Worksheet Basics
A workbook consists of worksheets. A worksheet is a grid of cells organized into 1,048,576 rows and 16,384 columns. Each row is numbered (“1”, “2”, “3”, …, “1048576”), and each column is lettered (“A”, “B”, “C”, …, “XFD”). Row and column headings are displayed at the left and at the top of a worksheet, respectively. Worksheet cells can contain formulas and different types of data.
Access a Worksheet
Use the Workbook.Worksheets property to access a collection of worksheets. The Workbook.Sheets property allows you to access a collection that contains both worksheets and chart sheets. You can obtain a specific worksheet by its index within these collections. A worksheet index is zero-based.
using DevExpress.Docs.Spreadsheet;
// Load a workbook from a file.
Workbook workbook = new Workbook(File.ReadAllBytes(@"..\..\..\Data\test_workbook.xlsx"));
// Access the first worksheet in the workbook.
Worksheet worksheet = workbook.Worksheets[0] as Worksheet;
// Do something with the worksheet.
// ...
For additional information on how to load spreadsheet documents, refer to the following help topic: Create, Load, and Save Workbooks.
To access a worksheet by its name, pass the name to the string indexer of the Workbook.Worksheets collection (SheetCollectionBase<T>.Item(name)). Sheet names are case-insensitive. The indexer returns null if the collection does not contain a worksheet with the specified name. Call the SheetCollectionBase<T>.Contains(name) method to check whether a sheet with the specified name exists.
Use the Workbook.ActiveSheet property to get or specify the active sheet. Microsoft Excel displays the active sheet when a user opens the workbook.
The following code snippet finds the Sales worksheet and makes it the active sheet:
var worksheet = workbook.Worksheets["Sales"];
if (worksheet != null)
workbook.ActiveSheet = worksheet;
Add a Worksheet
Create a Worksheet instance, and call the Add method to add the worksheet to the Workbook.Worksheets collection. After you add the worksheet to Workbook.Worksheets, API adds it to the Workbook.Sheets collection automatically and vice versa.
using DevExpress.Docs.Spreadsheet;
// Add a new worksheet with an auto-generated name ("Sheet{Index}").
Worksheet s = new Worksheet();
workbook.Worksheets.Add(s);
// Add a new worksheet with a specified name.
Worksheet s2 = new Worksheet("Worksheet2");
workbook.Worksheets.Add(s2);
If a worksheet instance has already been added to the collection, further Add calls only move this object to the end of the collection and do not create a copy.
For more information about naming conventions for worksheets, refer to the following section: Rename a Worksheet.
Insert a Worksheet to a Workbook
Call the Insert method to place a worksheet at a specific zero-based index. The following code places the Summary worksheet into the beginning of the workbook’s Worksheets collection:
using DevExpress.Docs.Spreadsheet;
// Insert a worksheet at the beginning of the collection.
workbook.Worksheets.Insert(0, new Worksheet("Summary"));
If a worksheet instance has already been added to the collection, further Insert calls only move this object to another position and do not create a copy.
Create a Worksheet Copy
Call the Clone method to create a copy of an existing worksheet:
using DevExpress.Docs.Spreadsheet;
Workbook workbook = new Workbook(File.ReadAllBytes(@"..\..\..\Data\Book1.xlsx"));
Worksheet worksheet = workbook.Worksheets[0].Clone();
worksheet.Name = "ClonedSheet";
Workbook workbook1 = new Workbook();
workbook1.Worksheets.Add(worksheet);
Rename a Worksheet
Set the worksheet’s Name property to rename a worksheet.
Worksheet names must follow the following rules:
- A worksheet name must not be equal to a name that is already contained in the collection of existing sheets (Workbook.Sheets).
- A worksheet name must not exceed 31 characters.
- A worksheet name must not contain the following symbols: \, /, ?, :, *, [ or ].
- The worksheet name must not start and end with a single quote (‘).
- A worksheet name must not be an empty string.
Note: When attempting to add a worksheet, an InvalidOperationException is thrown if another sheet with the same name already exists.
Manage the Worksheet Visibility
Set the Sheet.Visibility property to hide a worksheet. Use SheetVisibility enumeration values to specify the visibility state. The following values are available:
| Value | Description |
|---|---|
Visible |
The worksheet is visible. |
Hidden |
The worksheet is hidden but can be made visible again in Excel. |
VeryHidden |
The worksheet is hidden and cannot be made visible again in Excel. |
using DevExpress.Docs.Spreadsheet;
workbook.Worksheets[0].Visibility = SheetVisibility.Hidden;
Remove Worksheets
Call Remove to remove a worksheet instance, or call RemoveAt to remove the worksheet at a zero-based index:
using DevExpress.Docs.Spreadsheet;
workbook.Worksheets.Remove(workbook.Worksheets[0]);
workbook.Worksheets.RemoveAt(0);
A workbook must contain at least one visible sheet. Worksheets and chart sheets count together. If the workbook contains a single visible sheet, Remove and RemoveAt methods do not remove this sheet and throw an InvalidOperationException. The API throws the same exception if you assign a hidden sheet to the collection indexer at the position of this sheet.
Reorder and Replace Sheets
Call the SheetCollection.ResetTo(items) method of the Workbook.Sheets collection to replace all workbook sheets with the specified sheets in a single call. The workbook gets the sheet order from the list and removes sheets that the list does not include.
To replace a single worksheet, assign a new worksheet to the indexer of the Workbook.Worksheets collection.
The following code snippet sorts workbook sheets by name and removes the Draft worksheet:
using System;
using System.Collections.Generic;
using System.IO;
using DevExpress.Docs.Spreadsheet;
Directory.CreateDirectory("output");
using Workbook workbook = new Workbook();
workbook.Worksheets[0].Name = "Summary";
workbook.Worksheets.Add(new Worksheet("Sales"));
workbook.Worksheets.Add(new Worksheet("Costs"));
workbook.Worksheets.Add(new Worksheet("Draft"));
List<Sheet> sheets = new List<Sheet>();
foreach (Sheet sheet in workbook.Sheets) {
if (sheet.Name != "Draft")
sheets.Add(sheet);
}
sheets.Sort((x, y) => string.Compare(x.Name, y.Name, StringComparison.OrdinalIgnoreCase));
workbook.Sheets.ResetTo(sheets);
foreach (Sheet sheet in workbook.Sheets)
Console.WriteLine(sheet.Name);
using FileStream stream = File.Create(Path.Combine("output", "Document.xlsx"));
workbook.Save(stream);
Access a Cell Range in a Worksheet
Call the Worksheet.GetRange method to obtain a specific range of cells from a worksheet:
using DevExpress.Docs.Spreadsheet;
CellRange range = workbook.Worksheets[0].GetRange(rowIndex:0, columnIndex:0, rowCount:10, columnCount:10);