Spout

 
 

PHP Library for ODS, XLSX & CSV Files

Open Source PHP library to create, read & manipulate spreadsheets from Excel® (XLSX, CSV) & OpenOffice™ (ODS).

What is Spout?

Spout is an open source PHP library that helps software developers in spreadsheet manipulation tasks such as reading and writing ODS, XLSX & CSV files. With Spout, developers can create spreadsheets from scratch with cell, row & border styling as well as load existing files for editing.

Spout is developer-friendly as it provides simple and integrated API to read & create different types of spreadsheets. It also allows to switch from one type of spreadsheet to other with minimum changes to the code. It is capable of handling small as well as very large spreadsheets files while keeping the memory usage really low. It only requires 3MB of memory for processing any spreadsheet file.

Previous Next

How to Install Spout PHP Library

Spout needs PHP version 7.1 or higher. Once prerequisite is met, install Spout from Composer.

Install Spout from Composer

$ composer require box/spout

PHP Library to Read & Write ODS, XLSX & CSV

Spout enables software developers to create spreadsheet files from scratch in 3 popular formats. While reading, Spout guesses the spreadsheet reader type based on the file extension. In case the extension is not standard, a specific reader can be created directly.

Read ODS via PHP

  1. Create a reader for ODS file format
  2. Pass the ODS file path to load method to read it
  3. Iterate over ODS worksheets via getSheetIterator()
  4. Iterate over worksheet rows via getRowIterator()
  5. Get cells of a ODS row to read or write

Read ODS via Spout - PHP Excel Library

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

$reader = ReaderEntityFactory::createODSReader('/path/to/file.ods');

$reader->open($filePath);

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

$reader->close();

Add Data to Existing Spreadsheet via PHP

Spout gives the developer the ability to update an existing spreadsheet by adding data to it. It’s a very common practice to add new data to an existing spreadsheet. Spout API does not keep full spreadsheet representation in memory in order to avoid memory issues while working with large spreadsheets. To modify a spreadsheet it creates a new instance similar to the existing one and adds required data to the new spreadsheet.

Reading Data from a Specific Sheet

Spout API enables PHP developers to access and read data from a specific sheet inside a spreadsheet.  Usually, a spreadsheet contains several worksheets. If a user is interested to read data from only one sheet and skip the other sheets. The users just need to know the name and position of the spreadsheet.