I'm using PHPExcel to read out an excel file uploaded by users of my application, when they do I want to read out the first row of every worksheet and return these as JSON back to the client.
However, when a user uploads a spreadsheet (with +/- 15K rows in it) PHPExcel tries to load the entire file, rather than just a portion of it. Is there any way I could get PHPExcel to only load a small part of the file instead?
I tried using filter as described in this question, however they don't seem to stop the library from parsing the whole thing in the background anyways. (When I clear out all rows from the file by hand and then load it in, this goes incredibly fast).
Here is the code I'm using:
How I load my worksheet
// Get file type
$fileType = PHPExcel_IOFactory::identify($filePath);
// Create a reader
$reader = PHPExcel_IOFactory::createReader($fileType);
$reader->setReadDataOnly(true);
$reader->setReadFilter(new WorkSheetFilter());
// Can take up to 60 seconds
$reader->load($filePath);
WorksheetFilter
class WorkSheetFilter implements PHPExcel_Reader_IReadFilter
{
/**
* The first row in a spreadsheet
* @var int
*/
const FIRST_ROW = 1;
/**
* On read cell, check if the row is the first row
* If not, it won't be added to the spreadsheet
* @param \PHPExcel_Worksheet_Column $column
* @param \PHPExcel_Worksheet_Row $row
* @param string $worksheetName
* @return bool
*/
public function readCell($column, $row, $worksheetName = '')
{
return ($row == self::FIRST_ROW);
}
}
EDIT
I ended up using Akeneo's Spreadsheet Parser, it's an incredibly lightweight XLSX parser, which reduced my execution time down by about 95% (seriously, it's insane). If you're only interested in reading out data, I highly recommend it.