doutang1873 2016-01-16 10:12
浏览 78

Php CSV导出UTF8编码导致不正确的Excel格式

i tired doing an csv export. I needed two languages (tamil and english) in the sheet using normal headers

    $now = gmdate("D, d M Y H:i:s");
    header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
    header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
    header("Last-Modified: {$now} GMT");
    header ( 'Content-Type: application/vnd.ms-excel') ;
    header("Content-Disposition: attachment;filename={$filename}");
    header("Content-Transfer-Encoding: binary");
    echo $content;

This was causing problems in the tamil characters being displayed. But the format of the excel was fine. So i used a UTF8 encoding to display the tamil characters as shown below.

    $now = gmdate("D, d M Y H:i:s");
    header('Pragma: public');
    header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header("Last-Modified: {$now} GMT");
    header('Content-Type: text/csv');
    header("Content-Disposition: attachment;filename={$filename}");
    header("Content-Transfer-Encoding: binary");
    echo chr(255) . chr(254) . mb_convert_encoding($content, 'UTF-16LE', 'UTF-8');

This helped with the characters and both languages are being displayed fine now but the format of the csv being generated is disrupted. Everything is being displayed in a single column. Is there anyway to solve this?

  • 写回答

0条回答 默认 最新

    报告相同问题?