0

I am trying to get the last row that contains data in an excel workbook. I've used the function getHighestDataRow() like other advices I got from the internet. But it only work for .xls file. When I save the file to .xlsx format, the function return the wrong value Below is my code:

$inputFileType = PHPExcel_IOFactory::identify($file);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setReadDataOnly(true);
$objReader->setLoadSheetsOnly(0);
$objPHPExcel = $objReader->load($file);
$sheet = $objPHPExcel->getSheet(0);     

$highestRow = $sheet->getHighestDataRow();
$highestColumn = $sheet->getHighestDataColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);     

I've been looking for this problem for hours but still can't find the answer.

pnuts
  • 58,317
  • 11
  • 87
  • 139
Jasmine
  • 13
  • 1
  • 5
  • Vague and meaningless question.... your code doesn't show anything related to your question, you don't give any indication of expected and actual results – Mark Baker Jun 05 '15 at 10:36

1 Answers1

0

The getHighestDataRow() method doesn't care whether the PHPExcel object with its worksheets was loaded from a file, or created using new.... if loaded from a file, it doesn't know whether an xls file or an xlsx file was used to create the PHPExcel object... so you're wrong about this. The call works on the cell collection, not on the file or file type in any way.

Nor is the code that you've posted using $sheet->getHighestDataRow() it's using $sheet->getHighestRow()

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • I think this is not a proper answer. The problem still there for other people even if it question wasnt formulated properly, while trying to get the last row it doesnt do that. How should we count the number of rows in a excel sheet with lets say... validations in 1000 rows but only 4 rows with real content?? – Andoitz Jordán Dec 06 '17 at 17:51
  • @AndoitzJordán if `getHighestDataRow()` doesn't return the row that you expect, that's because it still considers cells that have invisible content (empty string or null are still valid data content for an Excel cell, even though you can't see anything in that cell, or with validations set for that cell) as cells. If you want to count only cells that you don't consider empty, then you have to write your own logic for that based on your own definition of empty – Mark Baker Dec 06 '17 at 17:59
  • I am using the very last version of PHPExcel but getHighestDataRow() doesnt seem to exist at all while getHighestRow() is working. Here my code: $sheet = $excel->setActiveSheetIndex(0); $highestRow = $sheet->getHighestDataRow(); – Andoitz Jordán Dec 06 '17 at 18:03
  • [It doesn't look undefined to me](https://github.com/PHPOffice/PHPExcel/blob/1.8/Classes/PHPExcel/Worksheet.php#L1060) What error are you getting? – Mark Baker Dec 06 '17 at 18:04