I need to generate a csv through PHP in UTF16-LE to support Excel (on Windows and Mac OS X). As suggested here I used mb_convert_encoding
and added the BOM at the file start, followed by sep=;
in order to make it open properly on Excel.
header('Content-Type: application/csv; charset=UTF-16LE');
header('Content-Disposition: attachment; filename=export.csv');
$output = fopen('php://output', 'w');
fputs($output, mb_convert_encoding("\xEF\xBB\xBF" . "sep=;\n" . implode($labels, ";") . "\n", 'UTF-16LE', 'UTF-8'));
foreach($data as $data_line) {
fputs($output, mb_convert_encoding(implode($data_line, ";") . "\n", 'UTF-16LE', 'UTF-8'));
}
The character encoding is ok, but when I try to open it in OpenOffice here is what I get:
The sep=;\n
isn't recognized - it shouldn't be on the first line. I don't think it's a BOM issue, because when I open it with an hex editor this is what I get:
The BOM seems to be correct, as it's ÿþ
which is the UTF16-LE BOM. I tried with \r\n
in place of \n
after sep, with no luck.