Retrieve a list of the hidden worksheets in a spreadsheet document

  • OpenXML SDK
  • Spire.XLS
  • Download Sample Code

class Program
    {
        static void Main(string[] args)
        {
            // Revise this path to the location of a file that contains hidden worksheets.
            const string DEMOPATH = @"..\..\Documents\Sheets11.xlsx";
            List sheets = GetHiddenSheets(DEMOPATH);
            foreach (var sheet in sheets)
            {
                Console.WriteLine(sheet.Name);
            }
            Console.ReadLine();
        }
        public static List GetHiddenSheets(string fileName)
        {
            List returnVal = new List();

            using (SpreadsheetDocument document = SpreadsheetDocument.Open(fileName, false))
            {
                WorkbookPart wbPart = document.WorkbookPart;
                var sheets = wbPart.Workbook.Descendants();

                // Look for sheets where there is a State attribute defined, 
                // where the State has a value,
                // and where the value is either Hidden or VeryHidden.
                var hiddenSheets = sheets.Where((item) => item.State != null &&
                    item.State.HasValue &&
                    (item.State.Value == SheetStateValues.Hidden ||
                    item.State.Value == SheetStateValues.VeryHidden));

                returnVal = hiddenSheets.ToList();
            }
            return returnVal;
        }
    }

class Program
    {
        static void Main(string[] args)
        {
            const string DEMOPATH = @"..\..\Documents\Sheets11.xlsx";
            List sheets = GetHiddenSheets(DEMOPATH);
            foreach (var sheet in sheets)
            {
                Console.WriteLine(sheet.Name);
            }
            Console.ReadLine();
        }
        public static List GetHiddenSheets(string fileName)
        {
            List returnVal = new List();

            //Initialize a new Workboook object
            Workbook workbook = new Workbook();

            //Load the document
            workbook.LoadFromFile(fileName);

            //Judge whether a sheet is hidden
            foreach (Worksheet sheet in workbook.Worksheets)
            {
                if (sheet.Visibility == WorksheetVisibility.Hidden)
                {
                    returnVal.Add(sheet);
                }
            }
            return returnVal;
        }
    }