How to retrieve the worksheet names

Hi,

How can we retrieve the worksheet names using Aspose.Cells. I have attached below a excel sheet which has three sheets with the names "Test1", "Test2" and "Test3". I was trying the following code snippet -

workbook = new Workbook();

workbook.Open(ExcelFileName);

string[] sNames = new string[workbook.Worksheets.Count];

Names worksheetNames = workbook.Worksheets.Names;

foreach (Name name in worksheetNames)

{

sNames[loop++] = name.Text;

}

return sNames;

However the collection worksheetNames did not have any element when I did a quickwatch and so it did not enter the for each loop.

Please let me know any alternative.

Thanks

Hemangajit

Hi,

Well, I think you slightly misunderstood Worksheets.Names. Basically it is used to get the collection of defined names (Named ranges defined in the worksheets) in a workbook. You may use Worksheet.Name property to get a worksheet name.

If you want to get each worksheet name only, you can modify your code a bit.

workbook = new Workbook();

workbook.Open(ExcelFileName);

string[] sNames = new string[workbook.Worksheets.Count];

int loop = 0;

foreach (Worksheet sheet in workbook.Worksheets)

{

sNames[loop++] = sheet.Name;

}

return sNames;

For further reference, please check the document:

http://www.aspose.com/documentation/file-format-components/aspose.cells-for-.net-and-java/managing-worksheets.html

Thank you.

Hi Amjad,

Thanks a lot. Worked wonder!