box/spout
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
Box/Spout is a highly efficient PHP library designed for reading and writing spreadsheet files. It's capable of handling all commonly used formats, including CSV, XLSX, and ODS. The feature that sets it apart is its performance with large files — it can process these while maintaining minimal memory usage, which is under 3MB. It's brilliant for those looking to work with large-scale data and manage spreadsheets in a quick, memory-friendly manner.
To use Box/Spout in your PHP projects, the first step is installing the package, which you can do via composer with the command composer require box/spout
. After installation, the library can be included in your script with the help of autoloading.
To read files, you can utilize the ReaderFactory
which provides methods to create a reader object according to your file type. Then, by using the open
method of this reader object, you can access rows of the spreadsheet.
<?php
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
$reader = ReaderEntityFactory::createReaderFromFile('/path/to/your/file.csv');
$reader->open();
foreach ($reader->getSheetIterator() as $sheet) {
foreach ($sheet->getRowIterator() as $row) {
$cells = $row->getCells();
// process cells
}
}
$reader->close();
When it comes to writing data, the WriterFactory
is your best tool. It provides methods to create a writer object suitable for your preferred file type. Use the addRow
function of this writer object to write data into files.
<?php
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
use Box\Spout\Common\Entity\Row;
$writer = WriterEntityFactory::createCSVWriter();
$writer->openToFile('/path/to/your/file.csv');
$data = ['data1', 'data2', 'data3']; // data to write
$row = WriterEntityFactory::createRowFromArray($data);
$writer->addRow($row);
$writer->close();
You can find complete instructions, explanations, and examples for each function in the Box/Spout documentation. The docs are found at the following URL: https://opensource.box.com/spout/. It's crucial to refer to these to fully understand how each component of this library works and to determine the best ways to use it in your PHP projects.