Hide and Unhide Worksheets in a Workbook in VSTO and Aspose.Cells
This article compares hiding and unhiding worksheets with VSTO, using either C# or Visual Basic, to performing the same task with Aspose.Cells, again using either C# or Visual Basic. Aspose.Cells lets you work without Microsoft Excel installed.
The steps to hide a worksheet are:
- Open a file.
- Get a worksheet.
- Hide the worksheet.
- Save the file. To unhide a worksheet again, simply toggle visibility on for the hidden sheet.
The code samples below first show how to hide a worksheet. The first samples show the process with VSTO, using either C#, compared to using Aspose.Cells, again using either C#.
The second set of code samples show the line used to unhide the worksheet in VSTO or Aspose.Cells.
Hiding Worksheets
Below are code samples for VSTO and Aspose.Cells that illustrate how to hide a worksheet in a workbook.
VSTO
//Instantiate the Application object.
Excel.Application excelApp = Application;
//Specify the template Excel file path.
string myPath = "Book1.xls";
//Open the Excel file.
excelApp.Workbooks.Open(myPath, Missing.Value, Missing.Value,
Missing.Value, Missing.Value,
Missing.Value, Missing.Value,
Missing.Value, Missing.Value,
Missing.Value, Missing.Value,
Missing.Value, Missing.Value,
Missing.Value, Missing.Value);
//Get the first sheet.
Excel.Worksheet objSheet = (Excel.Worksheet)excelApp.ActiveWorkbook.Sheets["Sheet1"];
//Hide the worksheet.
objSheet.Visible = Excel.XlSheetVisibility.xlSheetHidden;
//Save As the Excel file.
excelApp.ActiveWorkbook.Save();
//Quit the Application.
excelApp.Quit();
Aspose.Cells
//Instantiate a new Workbook.
Workbook workbook = new Workbook();
//Specify the template Excel file path.
string myPath = "Book1.xls";
//Open the Excel file.
workbook.Open(myPath);
//Get the first sheet.
Aspose.Cells.Worksheet objSheet = workbook.Worksheets["Sheet1"];
//Hide the worksheet.
objSheet.IsVisible = false;
//Save As the Excel file.
workbook.Save("Book1.xls");
UnHiding Worksheet
Below are code samples for VSTO and Aspose.Cells that illustrate how to unhide a worksheet in a workbook.
VSTO
//Unhide the worksheet.
objSheet.Visible = Excel.XlSheetVisibility.xlSheetVisible;
Aspose.Cells
//Unhide the worksheet.
objSheet.IsVisible = true;