How to determine the number of worksheets in an Excel workbook?

in advance for your help. I want to see all the worksheets in the book. Unfortunately, I do not know how many sheets are in this book. Now I use the following method to list all sheets:

Excel.Worksheet xlWorkSheet1;
xlWorkSheet1 = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
Excel.Worksheet xlWorkSheet2;
xlWorkSheet2 = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);
Excel.Worksheet xlWorkSheet3;
xlWorkSheet3 = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(3);

Is there a method that returns the number of worksheets in a book?

+3
source share
3 answers

Use Excel.Workbook, and then you can use Workbook.Sheets.Count()in a foror loop while.

+4
source

The Worksheets property contains a collection of worksheets.

foreach (Excel.Worksheet xlworksheet in xlworkbook.Worksheets)
{
    //xlworksheet code here
}

Or, get the value of count and use a for loop.

0
source

The activation of this result occurs either on this page, or on several others, where you must use VBA or C # or start working in the Name Manager in Excel.

Anyone looking for a quick and dirty solution:

Open the xlsx file with 7zip and go to xl \ worksheets. There you should see all the sheets, and if you select those 7zip, they will count them for you.

0
source

Source: https://habr.com/ru/post/1793912/


All Articles