Spout CSV / XLSX / ODS library

This project is not covered by Drupal’s security advisory policy.

Spout module integrates the Spout library with Drupal.

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

Supported Spreadsheet Types

Spout supports 3 types of spreadsheets: XLSX, ODS and CSV.
Spout provides a simple and unified API to read or create these different types of spreadsheets. Switching from one type to another is ridiculously easy!

Fast and Scalable

  • Reading a small CSV file? No problem!
  • Reading a huge XLSX file? No extra code needed!
  • Writing an ODS file with millions of rows? Spout can do it in no time!

Why use Spout?

  • Spout is capable of processing files of any size.
  • Spout needs only 3MB of memory to process any file.
  • Spout's streaming mechanism makes it incredibly fast.
  • Spout's API is developer-friendly.

Requirements

  • PHP version 5.4.0 or higher
  • PHP extension php_zip enabled
  • PHP extension php_xmlreader enabled

Dependencies

In order for this module to work, you must download the Spout library.

This module depends on the Libraries API module.

Installation

  • Download Spout library, unzip and rename folder to "spout".
  • Put the folder in a libraries directory (example: sites/all/libraries/spout).
  • Install and enable Libraries API 2.x.
  • Install and enable Spout module.

(Module installation help)

Basic usage

Reader

Regardless of the file type, the interface to read a file is always the same:

use Box\Spout\Reader\ReaderFactory;
use Box\Spout\Common\Type;

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

$reader->open($filePath);

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

$reader->close();

If there are multiple sheets in the file, the reader will read all of them sequentially.

Writer

As with the reader, there is one common interface to write data to a file:

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();

For XLSX and ODS files, the number of rows per sheet is limited to 1,048,576. By default, once this limit is reached, the writer will automatically create a new sheet and continue writing data into it.

Advanced usage

You can do a lot more with Spout! Check out the full documentation to learn about all the features.

Supporting organizations: 

Project information

Releases