Retrieve a list of the hidden worksheets in a spreadsheet document

2016-01-15 06:50:07 Written by  support iceblue
Rate this item
(0 votes)

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;
        }
    }