PhpSpreadsheet/samples/Basic/28_Iterator.php

36 lines
1.2 KiB
PHP
Raw Permalink Normal View History

<?php
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XLsxReader;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XLsxWriter;
2017-10-01 08:48:59 +00:00
require __DIR__ . '/../Header.php';
2017-10-01 08:48:59 +00:00
$sampleSpreadsheet = require __DIR__ . '/../templates/sampleSpreadsheet.php';
$filename = $helper->getTemporaryFilename();
$writer = new XlsxWriter($sampleSpreadsheet);
$callStartTime = microtime(true);
$writer->save($filename);
$helper->logWrite($writer, $filename, $callStartTime);
$callStartTime = microtime(true);
$reader = new XlsxReader();
$spreadsheet = $reader->load($filename);
2016-10-06 11:39:10 +00:00
$helper->logRead('Xlsx', $filename, $callStartTime);
unlink($filename);
$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) {
2017-10-01 11:07:04 +00:00
if ($cell !== null) {
$helper->log(' Cell - ' . $cell->getCoordinate() . ' - ' . $cell->getCalculatedValue());
}
}
}
}