我有點解決了導出表格爲包含中文的csv的問題。但不是真的與phpmyadmin。您可以使用此導出腳本導出需要包含中國表,它會顯示正確
<?php
/*
* PHP code to export MySQL data to CSV
* http://salman-w.blogspot.com/2009/07/export-mysql-data-to-csv-using-php.html
*
* Sends the result of a MySQL query as a CSV file for download
*/
/*
* establish database connection
*/
$conn = mysql_connect('MYSQL_HOST', 'MYSQL_USERNAME', 'MYSQL_PASSWORD') or die(mysql_error());
mysql_select_db('MYSQL_DATABASE', $conn) or die(mysql_error($conn));
mysql_query("SET character_set_results=utf8", $conn);
/*
* execute sql query
*/
$query = sprintf('SELECT * FROM MYSQL_TABLE');
$result = mysql_query($query, $conn) or die(mysql_error($conn));
/*
* send response headers to the browser
* following headers instruct the browser to treat the data as a csv file called export.csv
*/
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=export.csv');
echo "\xEF\xBB\xBF";
/*
* output header row (if atleast one row exists)
*/
$row = mysql_fetch_assoc($result);
if ($row) {
echocsv(array_keys($row));
}
/*
* output data rows (if atleast one row exists)
*/
while ($row) {
echocsv($row);
$row = mysql_fetch_assoc($result);
}
/*
* echo the input array as csv data maintaining consistency with most CSV implementations
* - uses double-quotes as enclosure when necessary
* - uses double double-quotes to escape double-quotes
* - uses CRLF as a line separator
*/
function echocsv($fields)
{
$separator = '';
foreach ($fields as $field) {
if (preg_match('/\\r|\\n|,|"/', $field)) {
$field = '"' . str_replace('"', '""', $field) . '"';
}
echo $separator . $field;
$separator = ',';
}
echo "\r\n";
}
?>
此代碼被複制,並從以下來源修改:
- http://salman-w.blogspot.hk/2009/07/export-mysql-data-to-csv-using-php.html
- When exported to CSV russian characters won't display
原來問題不在csv文件中,而是在excel中。不管csv文件的編碼是什麼,excel總是將它打開爲ASCII,這就把中文搞砸了。上面的代碼將
- 出口CSV爲UTF-8編碼,以便中國可以用這個
mysql_query("SET character_set_results=utf8", $conn);
- 力顯示擅長與此
echo "\xEF\xBB\xBF";
雖然這打開CSV爲UTF-8有點解決了這個問題,如果不需要自定義導出腳本,通過phpmyadmin就可以知道如何做到這一點。
任何人?........ – 2013-03-25 08:50:46
同樣的問題在這裏..仍然未解決.. – rusly 2013-04-25 14:42:07