2

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?

kjones
  • 1,339
  • 1
  • 13
  • 28
Pixelknight1398
  • 537
  • 2
  • 10
  • 33

1 Answers1

1

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?

Adrien
  • 1,929
  • 1
  • 13
  • 23
  • 1
    Yes it was a composer update thing I needed to do. I had version 2.7 of box/spout and composer kept installing that so I just updated php to the required version, and then updated the composer box/spout package and everything worked properly after that. Thank you. – Pixelknight1398 Sep 06 '19 at 05:39