Sheets Visibility
Sheets Visibility is a mechanism that allows you to show and hide sheets in a workbook. The following sections describe how to hide and unhide sheets.
Hiding Sheets
There are two available options when hiding sheets. The first option is to set the sheet Visibility property exposed both by the SheetCollection and WorksheetCollection classes. This property is of type SheetVisibility, an enum describing the visibility states of sheets:
| Value | Description |
|---|---|
Visible | The sheet is visible. |
Hidden | The sheet is hidden. Designed for UI purposes. |
VeryHidden | The sheet is very hidden. Can only be set through the API. |
Example 1: Set the SheetVisibility to Hidden or VeryHidden
//Change the visibility of the active sheet
workbook.ActiveSheet.Visibility = SheetVisibility.Hidden;
//OR change the visibility of the first sheet from the SheetCollection
workbook.Sheets[0].Visibility = SheetVisibility.Hidden;
//OR change the visibility of the first sheet from the WorksheetCollection
workbook.Worksheets[0].Visibility = SheetVisibility.Hidden;
The other option is to use the Hide method exposed by both the SheetCollection and WorksheetCollection classes. The Hide method provides several overloads and supports hiding a sheet by passing:
- A sheet at the specified index
- A specified sheet
- An active sheet (available on
SheetCollectiononly)
Example 2: Hide Sheets Using the Hide Method
Worksheet worksheet = workbook.Worksheets.First();
//Hiding the first sheet from the WorksheetCollection
workbook.Worksheets.Hide(worksheet);
Unhiding Sheets
As with hiding, you can unhide sheets both through setting the sheet Visibility property, or by using the Unhide method exposed by the SheetCollection and WorksheetCollection classes.
The following code snippets show the two approaches.
Example 3: Set the SheetVisibility to Visible
//Change the visibility of the first sheet from the SheetCollection
workbook.Sheets[0].Visibility = SheetVisibility.Visible;
The Unhide method provides two overloads and supports unhiding a sheet by:
- A sheet at the specified index
- A specified sheet
Example 4: Unhide Sheets Using the Unhide Method
//Unhiding sheet at the specified index
workbook.Worksheets.Unhide(0);
//OR unhiding the first sheet from the WorksheetCollection
workbook.Worksheets.Unhide(workbook.Worksheets.First());