csv to excel conversion

Using PHPExcel

include 'PHPExcel/IOFactory.php';

$objReader = PHPExcel_IOFactory::createReader('CSV');

// If the files uses a delimiter other than a comma (e.g. a tab), then tell the reader
$objReader->setDelimiter("\t");
// If the files uses an encoding other than UTF-8 or ASCII, then tell the reader
$objReader->setInputEncoding('UTF-16LE');

$objPHPExcel = $objReader->load('MyCSVFile.csv');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('MyExcelFile.xls');

Note: PHPExcel is now listed as DEPRECATED.

Users are directed to PhpSpreadsheet.


PHPExcel is deprecated. You must use PhpSpreadsheet.

With PhpSpreadsheet, you can convert csv to xlsx by the following code:

$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Csv');

// If the files uses a delimiter other than a comma (e.g. a tab), then tell the reader
// $reader->setDelimiter("\t");
// If the files uses an encoding other than UTF-8 or ASCII, then tell the reader
// $reader->setInputEncoding('UTF-16LE');

$objPHPExcel = $reader->load('/home/superman/projects/test/csv-app/file.csv');
$objWriter = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($objPHPExcel, 'Xlsx');
$objWriter->save('excel_file.xlsx');