PhpSpreadsheet/samples/Basic/16_Csv.php

39 lines
1.1 KiB
PHP
Raw Normal View History

<?php
use PhpOffice\PhpSpreadsheet\IOFactory;
2017-10-01 08:48:59 +00:00
require __DIR__ . '/../Header.php';
$spreadsheet = require __DIR__ . '/../templates/sampleSpreadsheet.php';
$helper->log('Write to CSV format');
$writer = IOFactory::createWriter($spreadsheet, 'Csv')->setDelimiter(',')
2018-01-28 06:59:38 +00:00
->setEnclosure('"')
->setSheetIndex(0);
$callStartTime = microtime(true);
$filename = $helper->getTemporaryFilename('csv');
$writer->save($filename);
$helper->logWrite($writer, $filename, $callStartTime);
$helper->log('Read from CSV format');
$reader = IOFactory::createReader('Csv')->setDelimiter(',')
2018-01-28 06:59:38 +00:00
->setEnclosure('"')
->setSheetIndex(0);
$callStartTime = microtime(true);
$spreadsheetFromCSV = $reader->load($filename);
$helper->logRead('Csv', $filename, $callStartTime);
2016-10-06 11:39:10 +00:00
// Write Xlsx
$helper->write($spreadsheetFromCSV, __FILE__, ['Xlsx']);
// Write CSV
$filenameCSV = $helper->getFilename(__FILE__, 'csv');
$writerCSV = IOFactory::createWriter($spreadsheetFromCSV, 'Csv');
$writerCSV->setExcelCompatibility(true);
$callStartTime = microtime(true);
$writerCSV->save($filenameCSV);
$helper->logWrite($writerCSV, $filenameCSV, $callStartTime);