Show and Hide Worksheets and Tabs

Show and Hide a Worksheet

An Excel file can have one or more than one worksheets. Whenever we create an Excel file, we add worksheets to the Excel file in which we work. Each worksheet in an Excel file is independent from the other worksheet by having its own data and formatting settings etc. Sometimes, developers may require to make few worksheets hidden and others visible in the Excel file for their own interest. So, Aspose.Cells allows developers to control the visibility of the worksheets in their Excel files.

Aspose.Cells provides a class, Workbook, that represents an Excel file. The Workbook class contains a Worksheets collection that allows access to each worksheet in the Excel file.

A worksheet is represented by the Worksheet class. The Worksheet class provides a wide range of properties and methods to manage worksheets. To control a worksheet’s visibility, use the IsVisible property of the Worksheet class. IsVisible is a Boolean property, which means that it can only store a true or false value.

Making a Worksheet Visible

Make a worksheet visible by setting the Worksheet class' IsVisible property to true

Hiding a Worksheet

Hide a worksheet by setting the Worksheet class' IsVisible property to false.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open);
// Instantiating a Workbook object with opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Hiding the first worksheet of the Excel file
worksheet.IsVisible = false;
// Shows first worksheet of the Excel file
//Worksheet.IsVisible = true;
// Saving the modified Excel file in default (that is Excel 2003) format
workbook.Save(dataDir + "output.out.xls");
// Closing the file stream to free all resources
fstream.Close();

Show and Hide Tabs

If you closely look at the bottom of a Microsoft Excel file, you will see a number of controls. These include:

  • Sheet tabs.
  • Tab scrolling buttons.

Sheet tabs represent the worksheets in the Excel file. Click any tab to switch to that worksheet. The more worksheets in the workbook, the more sheet tabs there are. If the Excel file has a good number of worksheets you need buttons to navigate through them. So, Microsoft Excel provides tab scrolling buttons for scrolling through the sheet tabs.

Using Aspose.Cells, developers can control the visibility of sheet tabs and tabs scrolling buttons in Excel files.

Aspose.Cells provides a class, Workbook, that represents an Excel file. The Workbook class provides a wide range of properties and methods to manage an Excel file. To control the visibility of tabs in an Excel file, developers can use the Workbook class' WorkbookSettings.ShowTabs property. WorkbookSettings.ShowTabs is a Boolean property, which means that it can only store a true or false value.

Making Tabs Visible

Make tabs visible with the Workbook class' WorkbookSettings.ShowTabs property to true.

Hiding Tabs

Hide tabs in an Excel file by setting the Workbook class' WorkbookSettings.ShowTabs property to false.

Below is a complete example that opens an Excel file (book1.xls), hides its tabs and saves the modified file as output.xls. After the code execution, you will see that the tabs of the workbook are hidden.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Opening the Excel file
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Hiding the tabs of the Excel file
workbook.Settings.ShowTabs = false;
// Shows the tabs of the Excel file
//workbook.Settings.ShowTabs = true;
// Saving the modified Excel file
workbook.Save(dataDir + "output.xls");

Controlling the Tab Bar Width

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Instantiating a Workbook object
// Opening the Excel file
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Hiding the tabs of the Excel file
workbook.Settings.ShowTabs = true;
// Adjusting the sheet tab bar width
workbook.Settings.SheetTabBarWidth = 800;
// Saving the modified Excel file
workbook.Save(dataDir + "output.xls");