I'm generating the csv file with the following header commands:
我正在用以下头命令生成csv文件:
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.
在Excel 2007如果我打开文件,然后我得到了££标志应该出现的地方。但是,如果我在Notepad++中打开文件,那么就会显示出良好的符号;类似地,如果我将内容类型更改为text/plain并去掉attachement头,那么在浏览器中会正确地出现pound标记。
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?
奇怪的是,如果我打开Notepad++的“Format”菜单,会发现文件被编码为“UTF-8,没有BOM”。如果我将其更改为“以UTF-8编码”,则保存该文件,在Excel中显示正确的磅符号。有没有一种方法可以让文件被PHP保存在这个编码中?
3 个解决方案
#1
11
Output 0xEF 0xBB 0xBF
before emitting the CSV data. Don't forget to increase the content length header by 3 if you handle it.
输出0xEF 0xBB 0xBF,然后发出CSV数据。如果你处理它,不要忘记将内容长度标头增加3。
header('Content-type: text/csv;');
header('Content-Length: ' + strlen($content) + 3);
header('Content-disposition: attachment;filename=UK_order_' . date('Ymdhis') . '.csv');
echo "\xef\xbb\xbf";
echo $content;
exit;
#2
9
Use utf8_decode() - WORKED FOR ME
使用utf8_decode() -为我工作
#3
0
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...
上面由user769889发布,但是我因为这个v-恼人的问题而错过了它,现在都已经修复并开始工作了。希望这可以帮助别人……
#1
11
Output 0xEF 0xBB 0xBF
before emitting the CSV data. Don't forget to increase the content length header by 3 if you handle it.
输出0xEF 0xBB 0xBF,然后发出CSV数据。如果你处理它,不要忘记将内容长度标头增加3。
header('Content-type: text/csv;');
header('Content-Length: ' + strlen($content) + 3);
header('Content-disposition: attachment;filename=UK_order_' . date('Ymdhis') . '.csv');
echo "\xef\xbb\xbf";
echo $content;
exit;
#2
9
Use utf8_decode() - WORKED FOR ME
使用utf8_decode() -为我工作
#3
0
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...
上面由user769889发布,但是我因为这个v-恼人的问题而错过了它,现在都已经修复并开始工作了。希望这可以帮助别人……