We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Getting number of columns returns strange value

Hello,

I have encountered the following issue. I am trying to get the number of columns in a worksheet. When the first row in the worksheet contains filters and one column which is not a filter then I get incorrect (or better say unexpected) value.

In the following example when the ‘example.xlsx’ is used, I expect 28 to be returned but instead 252 is returned:


public static int getColumns(Workbook workbook) {
Worksheet worksheet = workbook.getWorksheets().get(0);
return worksheet.getCells().getCount();
}



I tested this with Aspose.Cells 8.1.2. When I try to process the document using the LightCells API then it seems to detect correctly that the sheet has only 28 columns.

Do you have any idea why the Cells.getCount() returns different value?
Thanks for help.

Regards,
Juraj Stanik

Hi Juraj,


Thank you for contacting Aspose support.

The value returned by the Cells.getCount method is correct because the method will return the number of cells which are 252 (9 Rows x 28 Columns) in your provided spreadsheet. If you wish to get the count of Columns and/or Rows, we would recommend you to use the following approach that gives the correct count.

Java

Workbook book = new Workbook(myDir + “example.xlsx”);
Worksheet sheet = book.getWorksheets().get(0);

sheet.getCells().deleteBlankColumns();
sheet.getCells().deleteBlankRows();

System.out.println(“Number of cells:” + sheet.getCells().getCount());
System.out.println(“Number of columns:” + sheet.getCells().getColumns().getCount());
System.out.println(“Number of rows:” + sheet.getCells().getRows().getCount());

Output

Number of cells:252
Number of columns:28
Number of rows:9

Please feel free to write back in case you have an ambiguity.

Hello Babar Raza,

Thank you for your answer. I did a mistake in my example. I was getting the strange amount of columns by invoking:


worksheet.getCells().getColumns().getCount();

For the uploaded example it returned 1729. But after deleting the blank rows and columns I get the expected result 28:



Worksheet worksheet = workbook.getWorksheets().get(0);

worksheet.getCells().deleteBlankColumns();

worksheet.getCells().deleteBlankRows();
return worksheet.getCells().getColumns().getCount();


Thank you very much again for your reply which helped me to fix the issue.


Kind regards,

Juraj

Hi Juraj,


Its good to know that you are up & running again. Please feel free to contact us anytime you need our further assistance.