28_Iterator.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. use PhpOffice\PhpSpreadsheet\IOFactory;
  3. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  4. require __DIR__ . '/../Header.php';
  5. $sampleSpreadsheet = require __DIR__ . '/../templates/sampleSpreadsheet.php';
  6. $filename = $helper->getTemporaryFilename();
  7. $writer = new Xlsx($sampleSpreadsheet);
  8. $callStartTime = microtime(true);
  9. $writer->save($filename);
  10. $helper->logWrite($writer, $filename, $callStartTime);
  11. $callStartTime = microtime(true);
  12. $reader = IOFactory::createReader('Xlsx');
  13. $spreadsheet = $reader->load($filename);
  14. $helper->logRead('Xlsx', $filename, $callStartTime);
  15. $helper->log('Iterate worksheets');
  16. foreach ($spreadsheet->getWorksheetIterator() as $worksheet) {
  17. $helper->log('Worksheet - ' . $worksheet->getTitle());
  18. foreach ($worksheet->getRowIterator() as $row) {
  19. $helper->log(' Row number - ' . $row->getRowIndex());
  20. $cellIterator = $row->getCellIterator();
  21. $cellIterator->setIterateOnlyExistingCells(false); // Loop all cells, even if it is not set
  22. foreach ($cellIterator as $cell) {
  23. if ($cell !== null) {
  24. $helper->log(' Cell - ' . $cell->getCoordinate() . ' - ' . $cell->getCalculatedValue());
  25. }
  26. }
  27. }
  28. }