16_Csv.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. use PhpOffice\PhpSpreadsheet\IOFactory;
  3. require __DIR__ . '/../Header.php';
  4. $spreadsheet = require __DIR__ . '/../templates/sampleSpreadsheet.php';
  5. $helper->log('Write to CSV format');
  6. /** @var \PhpOffice\PhpSpreadsheet\Writer\Csv $writer */
  7. $writer = IOFactory::createWriter($spreadsheet, 'Csv')->setDelimiter(',')
  8. ->setEnclosure('"')
  9. ->setSheetIndex(0);
  10. $callStartTime = microtime(true);
  11. $filename = $helper->getTemporaryFilename('csv');
  12. $writer->save($filename);
  13. $helper->logWrite($writer, $filename, $callStartTime);
  14. $helper->log('Read from CSV format');
  15. /** @var \PhpOffice\PhpSpreadsheet\Reader\Csv $reader */
  16. $reader = IOFactory::createReader('Csv')->setDelimiter(',')
  17. ->setEnclosure('"')
  18. ->setSheetIndex(0);
  19. $callStartTime = microtime(true);
  20. $spreadsheetFromCSV = $reader->load($filename);
  21. $helper->logRead('Csv', $filename, $callStartTime);
  22. // Write Xlsx
  23. $helper->write($spreadsheetFromCSV, __FILE__, ['Xlsx']);
  24. // Write CSV
  25. $filenameCSV = $helper->getFilename(__FILE__, 'csv');
  26. /** @var \PhpOffice\PhpSpreadsheet\Writer\Csv $writerCSV */
  27. $writerCSV = IOFactory::createWriter($spreadsheetFromCSV, 'Csv');
  28. $writerCSV->setExcelCompatibility(true);
  29. $callStartTime = microtime(true);
  30. $writerCSV->save($filenameCSV);
  31. $helper->logWrite($writerCSV, $filenameCSV, $callStartTime);