Is there a way to convert csv file to excel file upon request through apache/.htaccess
-
3Please clarify what you mean. Do you mean faking an excel file using content-type headers, or actually converting the contents? – Pekka Oct 06 '10 at 16:39
-
I can't see any benefit to translating CSV to XLS unless you were to make some style-based changes, put in macros, or something that would fit the bill of why you need an XLS file. It's like you've gone out of your way to lessen portability of the file. – Incognito Oct 07 '10 at 14:06
-
1@Incognito One reason you'd want to convert from CSV to XLS is because office mobile on iOS doesn't support CSV files. So for someone to be able to view an emailed report file on their phone, it must be in an excel format. Apple's Numbers spreadsheet program supports them, but Office Mobile does not. – Ricky Sep 05 '14 at 15:36
6 Answers
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');

- 4,057
- 2
- 26
- 46

- 209,507
- 32
- 346
- 385
-
I just downloaded phpexcel for this exact purpose. thanks for the concise example, it helped me get things working much faster. – But those new buttons though.. Jun 18 '14 at 23:00
-
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');

- 2,745
- 2
- 20
- 32

- 2,578
- 26
- 18
Note: PHPExcel is now listed as DEPRECATED.
Users are directed to PhpSpreadsheet.

- 665
- 7
- 7
Using PhpSpreadheet:
require '../../vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
/* Set CSV parsing options */
$reader->setDelimiter(',');
$reader->setEnclosure('"');
$reader->setSheetIndex(0);
/* Load a CSV file and save as a XLS */
$spreadsheet = $reader->load('../../uploads/test.csv');
$writer = new Xlsx($spreadsheet);
$writer->save('test.xlsx');
$spreadsheet->disconnectWorksheets();
unset($spreadsheet);
This one worked for me like a charm ! Cheers !!!

- 6,819
- 3
- 29
- 33
Yes, since apache is open-source, you can modify the .htaccess parser to call a library to convert your CSV files into excel files. But I don't think this is what you're looking for. :-).
I think really what you need is a dynamic web site. Then you can use PHP or any supported language to do what you need to do.
something like this: http://www.westwideweb.com/wp/2009/01/12/convert-csv-to-xls-excel-in-php/

- 5,598
- 4
- 32
- 41
There is a project in sourceforge that does this conversion:
http://sourceforge.net/projects/py-csv2xls/
But for the conversion you need to make a dynamic page in apache (in python, php...)

- 131
- 1
- 2
- 6