Update 2019-06-01:
PHPExcel library has been deprecated. Use PhpSpreadsheet instead. PhpSpreadsheet is the next version of PHPExcel. I have converted the code below using PhpSpreadsheet library. The code can be found here.
Setup
- Download version 1.8.2 from https://github.com/PHPOffice/PHPExcel/releases.
- Uncompress the file.
- Change the paths of PHPExcel library in the code below to match yours.
Export Excel file to CSV
<?php /** Description: Export Excel file to CSV using PHPExcel library. PHPExcel library: https://github.com/PHPOffice/PHPExcel **/ // Error reporting error_reporting(E_ALL); ini_set('display_errors', TRUE); ini_set('display_startup_errors', TRUE); // Path to PHPExcel classes require_once './PHPExcel-1.8.2/Classes/PHPExcel.php'; require_once './PHPExcel-1.8.2/Classes/PHPExcel/IOFactory.php'; // Your input Excel file. $excelFile = './my-excel-file.xls'; // Create new PHPExcel object $objPHPExcel = new PHPExcel(); // Read your Excel workbook try { $inputFileType = PHPExcel_IOFactory::identify($excelFile); $objReader = PHPExcel_IOFactory::createReader($inputFileType); $objPHPExcel = $objReader->load($excelFile); } catch(Exception $e) { die('Error loading file "'.pathinfo($excelFile,PATHINFO_BASENAME).'": '.$e->getMessage()); } // Export to CSV file. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV'); $objWriter->setSheetIndex(0); // Select which sheet. $objWriter->setDelimiter(';'); // Define delimiter $objWriter->save('my-excel-file.csv'); echo "done"; ?>
Output
References
- http://stackoverflow.com/questions/3895819/csv-export-import-with-phpexcel
- http://stackoverflow.com/questions/9695695/how-to-use-phpexcel-to-read-data-and-insert-into-database
- http://stackoverflow.com/questions/6346314/phpexcel-will-not-export-to-csv