PHP 生成的 csv 文件显示 £ Excel 2007 中的英镑符号 (£)

发布于 2024-09-30 14:02:30 字数 448 浏览 1 评论 0原文

我使用以下标头命令生成 csv 文件:

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

如果我在 Excel 2007 中打开该文件,则只要出现 £ 符号,我就会得到 £。但是,如果我在 Notepad++ 中打开该文件,则井号显示正常;同样,如果我将内容类型更改为文本/纯文本并删除附件标题,井号将在浏览器中正确显示。

一件奇怪的事情是,如果我进入 Notepad++ 中的“格式”菜单,则该文件似乎是用“UTF-8 without BOM”编码的。如果我将其更改为“以 UTF-8 编码”,然后保存文件,井号将在 Excel 中正确显示。有没有办法让PHP以这种编码保存文件?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

枕花眠 2024-10-07 14:02:30

在发出 CSV 数据之前输出 0xEF 0xBB 0xBF。如果处理的话,不要忘记将内容长度标头增加 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;

Output 0xEF 0xBB 0xBF before emitting the CSV data. Don't forget to increase the content length header by 3 if you handle it.

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;
毁梦 2024-10-07 14:02:30

使用 utf8_decode() - 为我工作

Use utf8_decode() - WORKED FOR ME

梦魇绽荼蘼 2024-10-07 14:02:30
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);

上面由 user769889 发布,但我因对这个恼人的问题感到沮丧而错过了它,现在所有问题都已修复并正在工作。希望这对某人有帮助...

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文