Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP generated csv file is displaying £ for a UK pound sign (£) in Excel 2007

Tags:

php

csv

excel

utf-8

I'm generating the csv file with the following header commands:

header("Content-type: text/csv; charset=utf-8; encoding=utf-8");
header('Content-Disposition: attachment; filename="products.csv"');

If I open the file in Excel 2007, then I get £ wherever a £ sign should appear. However, if I open the file in Notepad++, then the pound signs appear fine; similarly, if I change the content-type to text/plain and get rid of the attachement header, the pound signs appear correctly in the browser.

One strange thing is that if I go to the "Format" menu in Notepad++, it appears that the file is encoded in "UTF-8 without BOM". If I change this to "Encode in UTF-8", then save the file, the pound signs appear correctly in Excel. Is there a way to make it so that the file is saved in this encoding by PHP?

like image 292
Gnuffo1 Avatar asked Nov 02 '10 10:11

Gnuffo1


2 Answers

header("Expires: Mon, 26 Nov 1962 00:00:00 GMT");
header("Last-Modified: " . gmdate('D,d M Y H:i:s') . ' GMT');
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header('Content-Type: text/csv;');
header("Content-Disposition: attachment; filename=".$savename);
echo utf8_decode($csv_string);

Posted as above by user769889 but I missed it with my frustrations with this v-annoying issue, all fixed now and working. Hope this helps someone...

like image 181
Mike Wells Avatar answered Oct 09 '22 14:10

Mike Wells


Use utf8_decode() - WORKED FOR ME

like image 42
user769889 Avatar answered Oct 09 '22 16:10

user769889