Search code examples
phpcomposer-phpspreadsheetspout

Box/Spout documentation not matching composer install?


I have been using the box/spout PHP spreadsheet library for quite some time now. An issue came up for me when I moved directories and reinstalled box/spout via typing:

composer require box/spout

Installation went fine, but a problem showed up when I tried to use the reader function:

use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;

$reader = ReaderEntityFactory::createReaderFromFile('/path/to/file.ext');

$reader->open($filePath);

foreach ($reader->getSheetIterator() as $sheet) {
    foreach ($sheet->getRowIterator() as $row) {
        // do stuff with the row
        $cells = $row->getCells();
    }
}

$reader->close();

I received this error:

Uncaught Error: Class 'ReaderEntityFactory' not found in /path/to/file/using/spout/script.php:182

So... At this point I am like okay I know I have been using this before so what's different... Here is the code I have been using:

require_once '/home/path/to/vendor/box/spout/src/Spout/Autoloader/autoload.php';
use Box\Spout\Reader\ReaderFactory;
use Box\Spout\Common\Type;

$reader = ReaderFactory::create(Type::XLSX); //for XLSX files
$reader->open($filepath);
$reader->setShouldFormatDates(true);

foreach ($reader->getSheetIterator() as $sheet) {
  foreach ($sheet->getRowIterator() as $row) {
      // do stuff with the row
      var_dump($row);

  }
}

$reader->close();

And this works great! Now I am not sure where I got this code. Possibly from the outdated documentation, possibly from another rogue tutorial page when I had this same issue in the past... Is this a composer issue possibly? Or am I just crazy?


Solution

  • It looks like a Composer issue. You don't seem to have pulled the latest version of Spout, hence the ReaderEntityFactory not found.

    Can you try running this command to check: composer show box/spout | grep versions?