James Flight
James Flight

Reputation: 1464

PHPExcel: using "getHighestRow" in combination with read filter

I'm currently using PHPExcel to read an excel file in chunks using a read filter, as the spreadsheet has the potential to become large in the future, and I need to ensure that enough memory will remain free to handle it regardless of size.

As part of the logic, I want to retrieve the number of rows in the sheet, which I'm aware can be done as follows:

$file = 'path/to/spreadsheet.xslx';
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load($file);
return $objPHPExcel->getActiveSheet()->getHighestRow();

My question is, is doing this going to cause PHP to run out of memory if the file is too large?

i.e. in the same way that it would if I used the following with a large file:

$objPHPExcel->getActiveSheet()->tooArray();

And if so, is there a way of retrieving the the highest row without causing a memory problem when the file becomes large?

I've tested this up as far as a 3MB file on my local machine, and it seems to cope fine, except takes and increasingly long time to return the highest row the bigger the file is.

Upvotes: 1

Views: 9181

Answers (1)

Mark Baker
Mark Baker

Reputation: 212412

You can retrieve the number of rows (or columns) in a worksheet without loading the whole file:

$file = 'path/to/spreadsheet.xslx';
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$worksheetData = $objReader->listWorksheetInfo($file);

echo '<h3>Worksheet Information</h3>';
echo '<ol>';
foreach ($worksheetData as $worksheet) {
    echo '<li>', $worksheet['worksheetName'], '<br />';
    echo 'Rows: ', $worksheet['totalRows'], 
        ' Columns: ', $worksheet['totalColumns'], '<br />';
    echo 'Cell Range: A1:', 
        $worksheet['lastColumnLetter'], $worksheet['totalRows'];
    echo '</li>';
}
echo '</ol>';

As shown in section 7 (Helper Methods) of the PHPExcel User Documentation - Reading Spreadsheet Files

Upvotes: 2

Related Questions