Retrieve a list of the worksheets in a spreadsheet document

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

class Program
    {
        static void Main(string[] args)
        {
            const string DEMOFILE = @"..\..\Documents\Sheets12.xlsx";
            var results = GetAllWorksheets(DEMOFILE);
            foreach (Sheet item in results)
            {
                Console.WriteLine(item.Name);
            }
            Console.ReadLine();
        }
        // Retrieve a List of all the sheets in a workbook.
        // The Sheets class contains a collection of 
        // OpenXmlElement objects, each representing one of 
        // the sheets.
        public static Sheets GetAllWorksheets(string fileName)
        {
            Sheets theSheets = null;

            using (SpreadsheetDocument document = SpreadsheetDocument.Open(fileName, false))
            {
                WorkbookPart wbPart = document.WorkbookPart;
                theSheets = wbPart.Workbook.Sheets;
            }
            return theSheets;
        }
    }