Retrieve a list of the worksheets in a spreadsheet document

  • OpenXML SDK
  • Spire.XLS
  • Download Sample Code

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

class Program
    {
        static void Main(string[] args)
        {
            const string DEMOFILE = @"..\..\Documents\Sheets12.xlsx";
            var results = GetAllWorksheets(DEMOFILE);
            foreach (Worksheet item in results)
            {
                Console.WriteLine(item.Name);
            }
            Console.ReadLine();
        }
        public static WorksheetsCollection GetAllWorksheets(string fileName)
        {
            //Initialize a new Workboook object
            Workbook workbook = new Workbook();

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

            //Get all sheets
            WorksheetsCollection worksheets = workbook.Worksheets;

            return worksheets;
        }
    }