How can I output a UTF-8 CSV in PHP that Excel will read properly?

后端 未结 30 2949
半阙折子戏
半阙折子戏 2020-11-22 06:08

I\'ve got this very simple thing that just outputs some stuff in CSV format, but it\'s got to be UTF-8. I open this file in TextEdit or TextMate or Dreamweaver and it displa

30条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 06:15

    In my case following works very nice to make CSV file with UTF-8 chars displayed correctly in Excel.

    $out = fopen('php://output', 'w');
    fprintf($out, chr(0xEF).chr(0xBB).chr(0xBF));
    fputcsv($out, $some_csv_strings);
    

    The 0xEF 0xBB 0xBF BOM header will let Excel know the correct encoding.

提交回复
热议问题