Skip to content

Instantly share code, notes, and snippets.

@madflow
Last active October 17, 2016 15:50
Show Gist options
  • Save madflow/c2ccef2409fac04544cf9c7537bbcfb6 to your computer and use it in GitHub Desktop.
Save madflow/c2ccef2409fac04544cf9c7537bbcfb6 to your computer and use it in GitHub Desktop.
<?php
require_once __DIR__.'/vendor/autoload.php';
use Box\Spout\Writer\WriterFactory;
use Box\Spout\Reader\ReaderFactory;
$testData = [
['Hi', 'There', ''],
[null, null, null],
['', 'Hi', 'There'],
['', '', ''],
];
function readAndDump($writerType, $testData) {
$writer = WriterFactory::create($writerType);
$writer->openToFile(__DIR__ . '/writer_'.$writerType.'.'.$writerType);
$writer->addRows($testData);
$writer->close();
$result = readTestFile(__DIR__ . '/writer_'.$writerType.'.'.$writerType, $writerType);
print $writerType.PHP_EOL;
print_r($result);
}
function readTestFile($fileName, $readerType)
{
$result = [];
/** @var \Box\Spout\Reader\XLSX\Reader $reader */
$reader = ReaderFactory::create($readerType);
$reader->setShouldPreserveEmptyRows(false);
$reader->open($fileName);
foreach ($reader->getSheetIterator() as $sheet) {
foreach ($sheet->getRowIterator() as $row) {
$result[] = $row;
}
}
$reader->close();
return $result;
}
readAndDump('ods', $testData);
readAndDump('csv', $testData);
readAndDump('xlsx', $testData);
/**
frosch ➜ spout-bug-finder php test.php
ods
Array
(
[0] => Array
(
[0] => Hi
[1] => There
[2] =>
)
[1] => Array
(
[0] =>
[1] =>
[2] =>
)
[2] => Array
(
[0] =>
[1] => Hi
[2] => There
)
)
csv
Array
(
[0] => Array
(
[0] => Hi
[1] => There
[2] =>
)
[1] => Array
(
[0] =>
[1] =>
[2] =>
)
[2] => Array
(
[0] =>
[1] => Hi
[2] => There
)
[3] => Array
(
[0] =>
[1] =>
[2] =>
)
)
xlsx
Array
(
[0] => Array
(
[0] => Hi
[1] => There
[2] =>
)
[1] => Array
(
[0] =>
[1] =>
[2] =>
)
[2] => Array
(
[0] =>
[1] => Hi
[2] => There
)
[3] => Array
(
[0] =>
[1] =>
[2] =>
)
)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment