Free Support Forum - aspose.com

Getting last row and column of table in aspose.cells

Hi,
I want to get last row and column present in a table in my sheet.
I want to extract the data from the table and for that I need to know the last row of that table .
I tried this code but it is giving Index out of Bound exception
Workbook workbook = new Workbook("/home/Downloads/file.xls");
WorksheetCollection worksheets = workbook.getWorksheets();
Worksheet worksheet =worksheets.get(0);
Cells cells = worksheet.getCells();
ListObjectCollection listObjectCollection = worksheet.getListObjects();
ListObject table = listObjectCollection.get(0);
table.convertToRange();
Range range = worksheets.getRangeByName(“table”);
System.out.println("First Row : " + range.getFirstRow());

P.S.-I am using the evaluation copy(not license).

Hi Vaibhav,


Thank you for considering Aspose APIs.

Please use the ListObject.getEndRow & getEndColumn fields to retrieve the indices of last row & column in a given table. Please check the following piece of code and the attached spreadsheet that I have used for my testing.

Regarding licensing, you can request for a 30 day temporary license to avoid any evaluation limitations.

Java

Workbook book = new Workbook(“D:/book1.xlsx”);
WorksheetCollection worksheets = book.getWorksheets();
Worksheet worksheet = worksheets.get(0);
ListObjectCollection listObjectCollection = worksheet.getListObjects();
ListObject table = listObjectCollection.get(0);

System.out.println(table.getEndRow());
System.out.println(table.getEndColumn());

Hi,


Thanks for providing some details.

Alternatively, I think you may try to use WorksheetCollection.getNamedRangesAndTables() method instead for your requirements. See the updated code segment for your reference:
e.g
Sample code:

Workbook workbook = new Workbook(“e:\test2\Book1.xlsx”);
Worksheet worksheet = workbook.getWorksheets().get(0);
ListObjectCollection listObjectCollection = worksheet.getListObjects();
ListObject table = listObjectCollection.get(0);

Range[] ranges = workbook.getWorksheets().getNamedRangesAndTables();


for (int i = 0; i < ranges.length; i++)
{
System.out.println(“Name: " + ranges[i].getName() + " Refer to: " + ranges[i].getRefersTo());
System.out.println(” First Row : " + ranges[i].getFirstRow());


}


Hope, this helps a bit.

Thank you.