在excel中以正确的编码导出UTF-8数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在excel中以正确的编码导出UTF-8数据相关的知识,希望对你有一定的参考价值。

If you want to export some utf-8 data into csv/tsv that will be readable by excel with correct encoding you must add special non vissable characters at the begining of file
  1. <?php
  2.  
  3. $data = "Some utf-8 characters ąćżźćł"
  4.  
  5. header("Content-Type: application/octet-stream");
  6. header("Content-Transfer-Encoding: binary");
  7. header('Expires: '.gmdate('D, d M Y H:i:s').' GMT');
  8. header('Content-Disposition: attachment; filename = "Export '.date("Y-m-d").'.tsv"');
  9. header('Pragma: no-cache');
  10.  
  11. //these characters will make correct encoding to excel
  12. echo chr(255).chr(254).iconv("UTF-8", "UTF-16LE//IGNORE", $data);
  13.  
  14. ?>

以上是关于在excel中以正确的编码导出UTF-8数据的主要内容,如果未能解决你的问题,请参考以下文章