PhpSpreadsheet/samples/28_Iterator.php
Adrien Crivelli 2922a13764
Reorganize code samples
This introduce a helper class that should be used to log things,
avoiding a lot of boilerplate code.

Also all output are made in /tmp folder instead of beside the script
itself. This is because there is a high chance that the folder containing
the script is not writtable by webserver. So using the /tmp folder
makes it more likely to works in a variety of setup.
2016-09-01 01:17:13 +09:00

32 lines
1.2 KiB
PHP

<?php
require __DIR__ . '/Header.php';
$sampleSpreadsheet = require __DIR__ . '/templates/sampleSpreadsheet.php';
$filename = $helper->getTemporaryFilename();
$writer = new \PhpSpreadsheet\Writer\Excel2007($sampleSpreadsheet);
$callStartTime = microtime(true);
$writer->save($filename);
$helper->logWrite($writer, $filename, $callStartTime);
$callStartTime = microtime(true);
$reader = \PhpSpreadsheet\IOFactory::createReader('Excel2007');
$spreadsheet = $reader->load($filename);
$helper->logRead('Excel2007', $filename, $callStartTime);
$helper->log('Iterate worksheets');
foreach ($spreadsheet->getWorksheetIterator() as $worksheet) {
$helper->log('Worksheet - ' . $worksheet->getTitle());
foreach ($worksheet->getRowIterator() as $row) {
$helper->log(' Row number - ' . $row->getRowIndex());
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false); // Loop all cells, even if it is not set
foreach ($cellIterator as $cell) {
if (!is_null($cell)) {
$helper->log(' Cell - ' . $cell->getCoordinate() . ' - ' . $cell->getCalculatedValue());
}
}
}
}