List All Worksheets in a Workbook

VSTO Excel

Excel.Application excelApp = Application;

            //Specify the template excel file path.
            string myPath = "List All Worksheets in a Workbook.xlsx";

            //Open the excel file.
            Microsoft.Office.Interop.Excel.Workbook ThisWorkbook = excelApp.Workbooks.Open(myPath, Missing.Value, Missing.Value,
                  Missing.Value, Missing.Value,
                  Missing.Value, Missing.Value,
                  Missing.Value, Missing.Value,
                  Missing.Value, Missing.Value,
                  Missing.Value, Missing.Value,
                  Missing.Value, Missing.Value);
            ListSheets(ThisWorkbook);

        }
        private void ListSheets(Microsoft.Office.Interop.Excel.Workbook workbook)
        {
            int index = 0;

            Excel.Range rng = this.Application.get_Range("A1", missing);

            foreach (Excel.Worksheet displayWorksheet in workbook.Worksheets)
            {
                rng.get_Offset(index, 0).Value2 = displayWorksheet.Name;
                index++;
            }
        }

Aspose Cells

static void Main(string[] args)
        {
            string myPath = "List All Worksheets in a Workbook.xlsx";
            Workbook workbook = new Workbook(myPath);

            ListSheets(workbook);
        }

        private static void ListSheets(Workbook workbook)
        {
            int index=0;
            Worksheet thisWorksheet = workbook.Worksheets[0];
            foreach (Worksheet worksheet in workbook.Worksheets)
            {
                thisWorksheet.Cells[index, 0].Value = worksheet.Name;
                index++;
            }
        }

Download

Last edited Feb 4, 2014 at 5:43 AM by asposemarketplace, version 2