Posted By

dsoms on 07/04/11


Tagged

excel utf-8 export encoding


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

candicom


Export UTF-8 data in excel with correct encoding


 / Published in: PHP
 

URL: http://www.php.net/manual/en/function.iconv.php#104287

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. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: candicom on November 12, 2012

nice work!! thanks!

You need to login to post a comment.