Tuesday, March 5, 2013

PHP - export uf-8 Chinese string to excel/csv encoding


A conversion is required before exporting it to output.

<?php

header('Content-type: application/vnd.ms-excel');
header("Content-Disposition: attachment; filename=file.xls");
header("Pragma: no-cache");

$buffer = $_POST['csvBuffer'];

echo chr(255) . chr(254) . mb_convert_encoding($buffer, 'UTF-16LE', 'UTF-8');

?>

here is the reference, http://php.net/manual/en/ref.mbstring.php

No comments: