PHPSpreadsheet: how to get the number of loaded rows?

Pontiac_CZ picture Pontiac_CZ · Feb 14, 2018 · Viewed 20.5k times · Source

How do I find out how many rows I have loaded using PHPSpreadsheet\Reader\Xlsx::load() method?

I cannot find methods (or properties) for getting row count in Spreadsheet or Worksheet classes either.

BTW I am using following code:

$filename = 'test.xlsx';
$inputFileType = \PhpOffice\PhpSpreadsheet\IOFactory::identify($filename);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);

$reader->setReadDataOnly(true);  
$reader->setLoadSheetsOnly($sheet);  

$this->spreadsheet = $reader->load($filename);
$this->worksheet = $this->spreadsheet->getActiveSheet();

Answer

Mark Baker picture Mark Baker · Feb 14, 2018

Using the worksheet's getHighestRow() method

$highestRow = $this->spreadsheet->getActiveSheet()->getHighestRow();

or getHighestDataRow() if you're only interested in rows where cells contain data and not any blank rows at the end of the worksheet