spout
Read and write spreadsheet files (CSV, XLSX and ODS), in a fast and scalable way
Top Related Projects
CSV data manipulation made easy in PHP
Faker is a PHP library that generates fake data for you
A pure PHP library for reading and writing spreadsheet files
A pure PHP library for reading and writing spreadsheet files
🦉 Fast Excel import/export for Laravel
Quick Overview
Spout is a PHP library for reading and writing spreadsheet files (CSV, XLSX, ODS). It provides a simple, memory-efficient way to process large files by using iterators and streaming techniques, making it suitable for handling big datasets without exhausting system resources.
Pros
- Memory-efficient, capable of handling large files
- Supports multiple spreadsheet formats (CSV, XLSX, ODS)
- Simple and intuitive API
- Actively maintained and well-documented
Cons
- Limited to basic spreadsheet operations (no advanced Excel features)
- Performance may be slower compared to native libraries for specific formats
- Requires PHP 7.2 or higher
Code Examples
Reading a CSV file:
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
$reader = ReaderEntityFactory::createCSVReader();
$reader->open('path/to/file.csv');
foreach ($reader->getSheetIterator() as $sheet) {
foreach ($sheet->getRowIterator() as $row) {
$cells = $row->getCells();
// Process the row data
}
}
$reader->close();
Writing to an XLSX file:
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
use Box\Spout\Common\Entity\Row;
$writer = WriterEntityFactory::createXLSXWriter();
$writer->openToFile('path/to/file.xlsx');
$rows = [
WriterEntityFactory::createRow(['Cell 1', 'Cell 2']),
WriterEntityFactory::createRow(['Cell 3', 'Cell 4']),
];
$writer->addRows($rows);
$writer->close();
Reading an ODS file with multiple sheets:
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
$reader = ReaderEntityFactory::createODSReader();
$reader->open('path/to/file.ods');
foreach ($reader->getSheetIterator() as $sheet) {
$sheetName = $sheet->getName();
foreach ($sheet->getRowIterator() as $row) {
// Process rows for each sheet
}
}
$reader->close();
Getting Started
To use Spout in your project, first install it via Composer:
composer require box/spout
Then, in your PHP code, you can start using Spout like this:
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
// Reading a file
$reader = ReaderEntityFactory::createReaderFromFile('path/to/file.xlsx');
$reader->open('path/to/file.xlsx');
// Writing a file
$writer = WriterEntityFactory::createWriterFromFile('path/to/file.csv');
$writer->openToFile('path/to/file.csv');
// Don't forget to close the reader and writer when done
$reader->close();
$writer->close();
Competitor Comparisons
CSV data manipulation made easy in PHP
Pros of CSV
- Lightweight and focused specifically on CSV handling
- Extensive CSV manipulation features (filtering, sorting, etc.)
- Easy to use with a simple and intuitive API
Cons of CSV
- Limited to CSV format only, unlike Spout's support for multiple formats
- May be slower for very large datasets compared to Spout's streaming approach
- Lacks some advanced features like formula support in Excel files
Code Comparison
CSV:
$reader = Reader::createFromPath('file.csv', 'r');
$reader->setHeaderOffset(0);
foreach ($reader as $record) {
// Process each record
}
Spout:
$reader = ReaderEntityFactory::createCSVReader();
$reader->open('file.csv');
foreach ($reader->getSheetIterator() as $sheet) {
foreach ($sheet->getRowIterator() as $row) {
// Process each row
}
}
Both libraries offer straightforward ways to read CSV files, but CSV provides a more concise approach for simple CSV handling. Spout's code structure reflects its ability to handle multiple file formats and sheets, which may be beneficial for more complex scenarios.
While CSV excels in dedicated CSV manipulation, Spout offers broader spreadsheet format support. The choice between the two depends on specific project requirements, such as file format diversity and performance needs for large datasets.
Faker is a PHP library that generates fake data for you
Pros of Faker
- Generates a wide variety of fake data types (names, addresses, phone numbers, etc.)
- Supports multiple locales for internationalized fake data
- Easy to use and integrate into existing projects
Cons of Faker
- Limited to generating in-memory data, not suitable for large-scale file operations
- Doesn't handle reading or writing to spreadsheet files
- Less performant for generating very large datasets
Code Comparison
Faker:
$faker = Faker\Factory::create();
$name = $faker->name;
$email = $faker->email;
$address = $faker->address;
Spout:
$writer = WriterFactory::createFromType(Type::XLSX);
$writer->openToFile($filePath);
$writer->addRow(['Name', 'Email', 'Address']);
$writer->addRow([$name, $email, $address]);
$writer->close();
Key Differences
- Faker focuses on generating fake data, while Spout specializes in reading and writing spreadsheet files
- Spout is more suitable for handling large datasets and file I/O operations
- Faker is better for quickly generating small to medium-sized sets of test data
- Spout supports various spreadsheet formats (CSV, XLSX, ODS), while Faker is not file-format specific
- Combining both libraries can be beneficial for generating and exporting fake data to spreadsheets
A pure PHP library for reading and writing spreadsheet files
Pros of PhpSpreadsheet
- More comprehensive feature set, supporting a wider range of Excel functions and formatting options
- Better support for complex spreadsheet operations and advanced formulas
- Actively maintained with regular updates and improvements
Cons of PhpSpreadsheet
- Higher memory usage, especially when dealing with large files
- Slower performance compared to Spout, particularly for reading and writing operations
- Steeper learning curve due to its more complex API and extensive functionality
Code Comparison
PhpSpreadsheet:
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World');
$writer = new Xlsx($spreadsheet);
$writer->save('hello_world.xlsx');
Spout:
$writer = WriterFactory::create(Type::XLSX);
$writer->openToFile('hello_world.xlsx');
$writer->addRow(['Hello World']);
$writer->close();
Both libraries offer solutions for working with spreadsheets in PHP, but they cater to different needs. PhpSpreadsheet provides a feature-rich environment for complex spreadsheet manipulation, while Spout focuses on simplicity and performance for basic read/write operations. The choice between them depends on the specific requirements of your project, balancing between functionality and resource efficiency.
A pure PHP library for reading and writing spreadsheet files
Pros of PhpSpreadsheet
- More comprehensive feature set, supporting a wider range of Excel functions and formatting options
- Better support for complex spreadsheet operations and advanced formulas
- Actively maintained with regular updates and improvements
Cons of PhpSpreadsheet
- Higher memory usage, especially when dealing with large files
- Slower performance compared to Spout, particularly for reading and writing operations
- Steeper learning curve due to its more complex API and extensive functionality
Code Comparison
PhpSpreadsheet:
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World');
$writer = new Xlsx($spreadsheet);
$writer->save('hello_world.xlsx');
Spout:
$writer = WriterFactory::create(Type::XLSX);
$writer->openToFile('hello_world.xlsx');
$writer->addRow(['Hello World']);
$writer->close();
Both libraries offer solutions for working with spreadsheets in PHP, but they cater to different needs. PhpSpreadsheet provides a feature-rich environment for complex spreadsheet manipulation, while Spout focuses on simplicity and performance for basic read/write operations. The choice between them depends on the specific requirements of your project, balancing between functionality and resource efficiency.
🦉 Fast Excel import/export for Laravel
Pros of Fast-Excel
- Significantly faster for large file operations due to its use of the libxlsxwriter C library
- Simpler API with fewer methods, making it easier to learn and use for basic tasks
- Smaller memory footprint, especially when dealing with large datasets
Cons of Fast-Excel
- Limited functionality compared to Spout, focusing mainly on writing Excel files
- Less flexibility in terms of customization options for cell formatting and styles
- Lacks built-in support for reading Excel files, which Spout provides
Code Comparison
Fast-Excel:
$fastExcel = new FastExcel();
$fastExcel->data([
['col1' => 'row1 col1', 'col2' => 'row1 col2'],
['col1' => 'row2 col1', 'col2' => 'row2 col2'],
])->export('file.xlsx');
Spout:
$writer = WriterFactory::create(Type::XLSX);
$writer->openToFile('file.xlsx');
$writer->addRow(['col1', 'col2']);
$writer->addRow(['row1 col1', 'row1 col2']);
$writer->addRow(['row2 col1', 'row2 col2']);
$writer->close();
Both libraries offer straightforward ways to create Excel files, but Fast-Excel's API is more concise for simple operations. Spout provides more granular control over the writing process and supports additional features like reading Excel files and streaming large datasets.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Spout
𪦠Archived project ðª¦
This project has been archived and is no longer maintained. No bug fix and no additional features will be added.
You won't be able to submit new issues or pull requests, and no additional features will be added
You can still use Spout as is in your projects though :)
Thanks to everyone who contributed to this project, from a typo fix to the new cool feature.
It was great to see the involvement of this community!
About
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).
Join the community and come discuss Spout:
Documentation
Full documentation can be found at https://opensource.box.com/spout/.
Requirements
- PHP version 7.2 or higher
- PHP extension
php_zip
enabled - PHP extension
php_xmlreader
enabled
Upgrade guide
Version 3 introduced new functionality but also some breaking changes. If you want to upgrade your Spout codebase from version 2 please consult the Upgrade guide.
Running tests
The master
branch includes unit, functional and performance tests.
If you just want to check that everything is working as expected, executing the unit and functional tests is enough.
phpunit
- runs unit and functional testsphpunit --group perf-tests
- only runs the performance tests
For information, the performance tests take about 10 minutes to run (processing 1 million rows files is not a quick thing).
Support
Spout is no longer actively supported. You can still ask questions, or discuss about it in the chat room:
Copyright and License
Copyright 2022 Box, Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Top Related Projects
CSV data manipulation made easy in PHP
Faker is a PHP library that generates fake data for you
A pure PHP library for reading and writing spreadsheet files
A pure PHP library for reading and writing spreadsheet files
🦉 Fast Excel import/export for Laravel
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot