Sheets Visibility
The purpose of this article is to describe what is Sheets Visibility. It contains the following sections:
What is Sheets Visibility?
Sheets Visibility is a mechanism to change the visibility of certain sheet, in order to be able to show and hide the relevant sheet(s);
Hiding Sheets
There are two available options when hiding sheets. The first option is by setting 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: Setting the SheetVisibility to Hidden/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 SheetCollection only)
Example 2: Hiding 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, unhiding can be done both through setting the Sheet Visibility property, or by using the Unhide method exposed by the SheetCollection and WorksheetCollection classes.
The following code snippets exemplify the two approaches.
Example 3: Setting 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: Unhiding 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());