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

后端 未结 30 2800
半阙折子戏
半阙折子戏 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:24

    I have the same (or similar) problem.

    In my case, if I add a BOM to the output, it works:

    header('Content-Encoding: UTF-8');
    header('Content-type: text/csv; charset=UTF-8');
    header('Content-Disposition: attachment; filename=Customers_Export.csv');
    echo "\xEF\xBB\xBF"; // UTF-8 BOM
    

    I believe this is a pretty ugly hack, but it worked for me, at least for Excel 2007 Windows. Not sure it'll work on Mac.

提交回复
热议问题