PHPSpreadsheet: how to get the number of loaded rows?

PHPSpreadsheet: how to get the number of loaded rows?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Determining Loaded Row Counts in PHPSpreadsheet

When working with PHPSpreadsheet, a common requirement is to know exactly how many rows of data were successfully loaded into memory after reading an Excel file. The standard workflow involves using the IOFactory to identify the file type, creating a reader instance, and loading the spreadsheet object. You might set options like setReadDataOnly(true) to optimize memory usage or setLoadSheetsOnly() to restrict processing to specific sheets. However, once $this->spreadsheet = $reader->load($filename); executes, there is no direct property on the Spreadsheet or Worksheet classes that exposes the count of rows currently populated with data.

This limitation exists because Excel files can contain a vast number of empty rows extending beyond the last cell with actual content. The physical file size often includes formatting for thousands of rows, but your application usually only cares about the contiguous block of data ending at the last filled row. Relying on file metadata or guessing based on sheet dimensions will yield incorrect results if you are processing a subset of the workbook.

To retrieve the number of loaded rows, you must query the specific worksheet object for its dimensions. The PhpOffice\PhpSpreadsheet\Worksheet\Worksheet class provides a method called getHighestRow(). This method returns an integer representing the index of the last row that contains at least one cell (whether it holds data or formatting). Since row indices in PHPSpreadsheet are 1-based, this value corresponds directly to the number of rows you need.

Here is how you integrate this into your existing logic:

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

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

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

// Get the index of the last row containing content
$totalRows = $this->worksheet->getHighestRow();

// If you need a 0-based count for array logic, subtract 1
$rowCount = $totalRows - 1; 

It is important to understand the distinction between getHighestRow() and getHighestColumn(). While getHighestRow gives you the vertical limit, it does not guarantee that every row in that range contains data. It simply marks the boundary of the sheet's active area. If your dataset has gaps—such as rows 1 through 50 containing data and row 51 being empty but formatted—the getHighestRow will still return 51 (or higher) if formatting exists there.

If you strictly need to know how many rows contain actual cell values rather than just the highest row index, you would have to iterate through the range defined by getHighestRow() and check each row individually using methods like getRowDimension($i)->getCellCount(). However, for most data processing tasks where contiguous blocks of data are expected, getHighestRow() is the standard approach.

One potential pitfall involves the setReadDataOnly(true) flag. When this option is enabled, PHPSpreadsheet reads only the cell values and skips reading formulas, comments, and hyperlinks. Crucially, it also ignores formatting that might extend beyond the last actual data row. In such cases, getHighestRow() will accurately reflect the extent of the loaded data without being inflated by empty formatted rows. If you disable this flag, the returned row count might increase artificially if the Excel file contains merged cells or formatting far below your actual dataset.

For developers working within a Laravel application, keeping these dependencies clean is essential. While there are external libraries that wrap PHPSpreadsheet functionality, sticking to the core PhpOffice\PhpSpreadsheet API ensures you have access to the most up-to-date methods and behaviors without relying on third-party abstractions that might hide implementation details. For more advanced spreadsheet manipulation or specific enterprise requirements, you might explore dedicated resources such as https://laravelcompany.com, which often discusses integration patterns for complex data handling tasks.

Our network