How to read a ods file with Spout in PHP?
Spout is a PHP library to read and write spreadsheet files (CSV, XLSX and ODS), in a fast and scalable way.
1. Code:
<?php use Box\Spout\Reader\Common\Creator\ReaderFactory; use Box\Spout\Common\Type; require_once 'spout/src/Spout/Reader/Common/Creator/ReaderFactory.php'; require_once 'spout/src/Spout/Autoloader/autoload.php'; $fileInput = 'C:\readOdsFile\example_upload_using_spout.ods'; $fileInputs = explode("\\", $fileInput); $fileName = end($fileInputs); $fileNames = explode(".", $fileName); $extension = end($fileNames); $fileUploadData = []; if (!empty($fileInput)) { if ($extension == 'ods') { #Read excel file by using ReadFactory object. $reader=ReaderFactory::createFromType(Type::ODS); $reader->open($fileInput); $count = 1; $rows = array(); foreach ($reader->getSheetIterator() as $sheet) { foreach ($sheet->getRowIterator() as $row) { $rowAsArray = $row->toArray(); if ($count > 0) { $fileUploadData[] = $rowAsArray; } $count++; } echo "successfully uploaded!"; } $reader->close(); } else { echo "PLEASE SELECT A VALID EXCEL FILE"; } } else { echo "UPLOAD AN EXCEL FILE"; } ?> <p><?php echo($fileName) ?></p> <table > <?php foreach($fileUploadData as $rows){?> <tr> <?php foreach($rows as $value){ ?> <td><?php echo($value); ?></td> <?php } ?> </tr> <?php } ?> </table> <style> table, td, th { border: 1px solid; } table { width: 100%; border-collapse: collapse; } </style>
2. Example:
Input: example_upload_using_spout.ods
ID Name Description 1 Spout Spout is a PHP library to read and write spreadsheet files (CSV, XLSX and ODS) 2 ReaderFactory This creates an instance of the appropriate reader, given the type of the file to be read
Output:
ID | Name | Description |
1 | Spout | Spout is a PHP library to read and write spreadsheet files (CSV, XLSX and ODS) |
2 | ReaderFactory | This creates an instance of the appropriate reader, given the type of the file to be read |