从MySQL DB将中文单词导出为CSV

问题描述 投票:1回答:2

这是我的代码:

mysql_set_charset("utf8");
mysql_query("SET NAMES utf8");
header('Content-Type: text/html; charset=utf-8');
mysql_connect("localhost","root","");
mysql_select_db("ladli");
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET COLLATION_CONNECTION = 'utf8_unicode_ci'");

$output         = "";
$table          = ""; 
$sql            = mysql_query("select * from table'");
mysql_query("SET NAMES utf8");
$columns_total  = mysql_num_fields($sql);

for ($i = 0; $i < $columns_total; $i++) {
$heading    =   mysql_field_name($sql, $i);
$output     .= '"'.$heading.'",';
}
$output .="\n";

// Get Records from the table

while ($row = mysql_fetch_array($sql)) {
for ($i = 0; $i < $columns_total; $i++) {

$output .='"'.$row["$i"].'",';

}
$output .="\n";
}

$filename =  "chinese_test.csv";

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 $output;
exit;

In my DB: 测试一下

我是csv:æ'æ〜¯

当我尝试使用PHP下载一个csv时,我搞乱了csv数据与不可读的格式“æ'æ〜”等。

我需要从MySQL数据库中导出csv / xsl格式的中文单词。

提前致谢。

php mysql csv utf-8
2个回答
0
投票

一个简单的解决方法是使用Google电子表格。粘贴(仅当您具有复杂公式时的值)或导入工作表然后下载CSV。我只是试了几个角色而且效果很好。

注意:Google表格在导入时确实存在限制。见here.

注意:请注意Google表格中的敏感数据。

编辑:另一个qazxsw poi - 基本上他们使用VBA宏或插件来强制保存为UTF8。我没有尝试过任何这些解决方案,但它们听起来很合理。


0
投票

qazxsw poi是qazxsw poi的“Mojibake”。有关该常见问题及其原因的讨论,请参阅qazxsw poi。

在中文中,一些字符需要4字节的UTF-8字符。为此,你需要MySQL中的alternative

© www.soinside.com 2019 - 2024. All rights reserved.