Hi ,
I am having issues in counting the number of sheets in excel file (.xlsx) format.
If excel is having only one sheet and that sheet is having chart with in it, below aspose code shows that excel contains 2 sheets.
Workbook workbook = new Workbook(“test.xlsx”);
WorksheetCollection colresultt = workbook.getWorksheets();
System.out.println(colresultt.getCount());
But we have a specific requirements, where we need sheet count should be the same as number of sheet in the excel file.
Using this version : aspose-cells-7.1.2.jar
Thanks,
Sachin Dagar
Hi Sachin,
Thanks for your posting and using Aspose.Cells for Java.
We have checked this issue and found that Aspose.Cells returns correct number of worksheets. Probably, one of your worksheet is hidden. Please provide us your test.xlsx file so that we could investigate your issue.
Please also make sure that none of your worksheets is hidden or very hidden because sheet count also takes into account the hidden worksheets.
Thanks, yes there is hidden sheet .
Can you please give some sample code to count only visible sheet and not the hidden one ?
Your help will be much appreciated.
Thanks,
Sachin Dagar
Hi Sachin,
Thanks for using Aspose.Cells.
You need to iterate all worksheets and calculate the count of visible worksheets by checking the Worksheet.isVisible() property. Please see the following code for your reference.
Java
String filePath = "F:\\Shak-Data-RW\\Downloads\\Book1.xlsx";
Workbook workbook = new Workbook(filePath);
int visibleSheetCount = 0;
for(int i=0; i<workbook.getWorksheets().getCount(); i++)
{
Worksheet sheet = workbook.getWorksheets().get(i);
if(sheet.isVisible())
visibleSheetCount++;
}
System.out.println("Number of Visible Sheets: " + visibleSheetCount);