1

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.

Community
  • 1
  • 1
Paradoxis
  • 4,471
  • 7
  • 32
  • 66

1 Answers1

2

Parsing and loading aren't quite the same thing.

The PHPExcel Reader loads all of the the cell data from the file, and then parses that to determine whether cells should be added to the PHPExcel object based on your filter. But unless it can parse that cell data list to get row/column/worksheet, then it can't tell whether that cell should be loaded to the PHPExcel object or not.

So the basic answer is "No", unless you have some clever way of identifying what cells you want to load, and are prepared to create your own Reader and/or filtering mechanism to do so.

Mark Baker
  • 209,507
  • 32
  • 346
  • 385