How to hide or show Excel worksheet in C#

Sometimes, you want to hide an excel worksheet to make your workbook tidy and clear. With Spire.XLS for .NET, you can hide any worksheet in a workbook to remove it from view. The data in hidden worksheets is not visible, but it can still be referenced from other worksheets. You can display the hidden worksheets when you need. This article will show you how to hide or show Excel worksheet in C#.

Step 1: Create a new document and load from file.

Workbook wb = new Workbook();
wb.LoadFromFile("Sample.xlsx");

Step 2: Call the class of WorksheetVisibility to set the property to hidden or visible for the worksheet.

//Hide Sheet2
wb.Worksheets[1].Visibility = WorksheetVisibility.Hidden;

//Show the sheet
wb.Worksheets[j].Visibility = WorksheetVisibility.Visible;

Step 3: Save the document to file.

wb.SaveToFile("Result.xlsx",ExcelVersion.Version2010);

Effective screenshot after hide sheet2:

How to hide or show Excel worksheet in C#

Full codes:

namespace HideShowExcelWorksheet
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook wb = new Workbook();
            wb.LoadFromFile("Sample.xlsx");
            wb.Worksheets[1].Visibility = WorksheetVisibility.Hidden;
            wb.SaveToFile("Result.xlsx",ExcelVersion.Version2010);
         }
    }
}