Code Documentation for Old Version 2.x of box/spout PHP Spreadsheet Package

Box.com publishes an open sources PHP spreadsheet package that is simple (less featureful than the old deprecated PHP-Excel package, and phpoffice/phpspreadsheet but much faster for operating on large spreadsheets):

Spout is a PHP library to read and write spreadsheet files (CSV, XLSX and ODS), in a fast and scalable way. Unlike other file readers or writers, it is capable of processing very large files, while keeping the memory usage really low (less than 3MB).

Many of the other PHP spreadsheet packages rely on loading the entire spreadhsheet file into memory to process it which is a terrible architecture for dealing with large spreadhseets. So if you don't require and of the more advanced spreadhseet features missing from Spout, it can be a great option.

It seems since they've released a new major version 3 that the documentation for the prior version 2 is gone. Version 3 requires PHP 7.2 or higher, so anyone stuck on a version of PHP below that is stuck on the old 2.x release.

In this stackoverflow question, there is a code example for reading files with the 2.x version.

Reading Files

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) {
        var_dump($row);
    }
}

$reader->close();

Setting the Filetype

//$reader = ReaderFactory::create(Type::XLSX); // for XLSX files
//$reader = ReaderFactory::create(Type::CSV); // for CSV files
//$reader = ReaderFactory::create(Type::ODS); // for ODS files

Writing Files

For now the old documentation can be accessed via archive.org but that's not a foolproof solution as the archive entries could disappear at any time for a variety of reasons.

For instance here is the "Getting Started" docs page from 2018/10/22, which includes a snipper for writing files using the version 2.x branch:

use Box\Spout\Writer\WriterFactory;
use Box\Spout\Common\Type;

$writer = WriterFactory::create(Type::XLSX); // for XLSX files
//$writer = WriterFactory::create(Type::CSV); // for CSV files
//$writer = WriterFactory::create(Type::ODS); // for ODS files

$writer->openToFile($filePath); // write data to a file or to a PHP stream
//$writer->openToBrowser($fileName); // stream data directly to the browser

$writer->addRow($singleRow); // add a row at a time
$writer->addRows($multipleRows); // add multiple rows at a time

$writer->close();

Tags

 box/spout  PHP packages  composer packages  Excel PHP  spreadsheets  PHP spreadsheets