I'm trying to export an array to a CSV file using PHP's fputcsv function. However I get two completely different behaviours when opening the file in Excel 2007 and 2010.
In Excel 2007 I get what I want since all the fields are in its own separate column like this:
aaa | bbb | ccc | dddd
123 | 456 | 789
"aaa" | "bbb" |
But in Excel 2010 all the fields are on the same column like this:
aaa,bbb,ccc,dddd
123,456,789
"aaa","""bbb"""
How can I get the Excel 2007 behaviour for both Excel 2007 and 2010?
I used the following script:
$filename = "test.csv";
//header("Content-Type: application/vnd.ms-excel;");
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header("Pragma: no-cache");
header("Expires: 0");
$list = array (
array('aaa', 'bbb', 'ccc', 'dddd'),
array('123', '456', '789'),
array('"aaa"', '"bbb"')
);
$fp = fopen("php://output", 'w');
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
I've tried using header("Content-Type: application/vnd.ms-excel;"); or header('Content-Type: text/csv'); but I get the same behaviour. :S
Thank you in advance.
EDIT: I just ended up using PHPExcel. It's a great library!!