当数据具有 UTF8 字符时,PHP 导出 CSV
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17592707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP export CSV when data having UTF8 charcters
提问by Sajal
In MySQL I have set my data field type to utf8_bin
and i am storing data in Unicode. Texts are being properly displayed in web pages.
在 MySQL 中,我已将数据字段类型设置utf8_bin
为 Unicode ,并且正在以 Unicode 格式存储数据。文本在网页中正确显示。
I want to generate excel file exporting data from my table to it. The output in .xls
and .cvs
is - '????'.
我想生成将数据从我的表导出到它的 excel 文件。输出中.xls
和.cvs
是- “????”。
I checkout out other answers here, its been referred to use headers:
我在这里查看其他答案,它被称为使用标题:
header("content-type:application/csv;charset=UTF-8");
header("content-type:application/csv;charset=UTF-8");
similar question. But its not working. After using header, in csv Output is - à¤?à¥?रà¥à¤ˉà¤?.
类似的问题。但它不起作用。使用 header 后,在 csv 输出是 - à¤?à¥?रà¥à¤ˉà¤?。
Please help. Thanks.
请帮忙。谢谢。
回答by Sajal
This post solved my problem: https://stackoverflow.com/a/4762708/2349494
这篇文章解决了我的问题:https: //stackoverflow.com/a/4762708/2349494
And this is what did the conversion:
这就是转换的内容:
print chr(255) . chr(254) . mb_convert_encoding($csv_output, 'UTF-16LE', 'UTF-8');
Earlier I was getting garbage characters now correct data is exported.
早些时候我得到了垃圾字符,现在正确的数据被导出。
回答by Poorya
I had the same problem and I did it by combining two things:
我遇到了同样的问题,我通过结合两件事来解决这个问题:
First, you have to change table collation to UTF8 e.g: "utf8_general_ci" (or just make sure of that), then add this code after your MySQL query:
首先,您必须将表排序规则更改为 UTF8 例如:“utf8_general_ci”(或者只是确保这一点),然后在您的 MySQL 查询之后添加此代码:
mysql_query("SET NAMES utf8");
Like
喜欢
$result = mysql_query("SHOW COLUMNS FROM table WHERE Field NOT IN ('user_id', 'password')");
mysql_query("SET NAMES utf8");
And then, use this as header (adapt with your needs):
然后,将其用作标题(适应您的需求):
header('Content-Description: File Transfer');
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header("Content-disposition: filename=".$filename.".csv");
header('Content-Transfer-Encoding: binary');
header('Pragma: public');
print "\xEF\xBB\xBF"; // UTF-8 BOM
print $csv_output;
exit;
回答by ldaguise
I have a variant to Sajal response and found it on php.net at : http://www.php.net/manual/en/function.iconv.php#104287
我有一个 Sajal 响应的变体,并在 php.net 上找到了它:http://www.php.net/manual/en/function.iconv.php#104287
Echo this at the begining of the content of the csv :
在 csv 内容的开头回显:
chr(255).chr(254).iconv("UTF-8", "UTF-16LE//IGNORE", $data)
I prefer with iconv()
but it seems it do the same as mb_convert_encoding()
.
Don't forget to replace ;
by tabs (\t
) and it worked fine for me like this !
我更喜欢 withiconv()
但它似乎与mb_convert_encoding()
. 不要忘记;
用制表符 ( \t
)替换,这样对我来说效果很好!
回答by Ashwin Parmar
Try following code;
尝试以下代码;
ob_end_clean();
$_filename = date('Y_m_d_H_i').".csv";
header("Cache-Control: public");
header("Content-Type: application/octet-stream");
header("Content-Type: text/csv; charset=utf-8");
header("Content-Disposition: attachment; filename=$_filename");
// Read the file from disk
readfile($filename);
exit();
回答by Zakari
export it as XLS (xml format) with unicode support, just tested and working 100%
将其导出为支持 unicode 的 XLS(xml 格式),刚刚测试并 100% 工作