用excel读取csv文件中的阿拉伯文时出错
问题描述:
我有这段代码将数据库表格导出为csv,我拥有这样的代码,它应该看起来像这样的علي。我不知道是什么问题,是因为编码还是什么。我的表归类为utf8_general_ci用excel读取csv文件中的阿拉伯文时出错
$mysqli = new mysqli('localhost', 'root', '', 'test');
if ($mysqli->connect_errno)
{
$debug = $debug.'<br/>Failed to connect to MySQL: (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error;
}
else
{
$debug = $debug.'<br/>Connected to '. $mysqli->host_info;
}
function export($query_exp,$name)
{
require 'classs.php';
$filename = $name.' - '.date('Y.m.d').'.xls'; /*set desired output file name here*/
function cleanData(&$str)
{
$str = preg_replace("/\t/", "\\t", $str);
$str = preg_replace("/\r?\n/", "\\n", $str);
if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
if ($str=='') {$str='-';}
}
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.ms-excel");
$flag = false;
if ($result = $mysqli->query($query_exp))
{
if ($result->num_rows>0)
{
$result->data_seek(0);
while ($row = $result->fetch_assoc())
{
if(!$flag)
{
print implode("\t", array_keys($row)) . "\r\n";
$flag = true;
}
array_walk($row, 'cleanData');
print implode("\t", array_values($row)) . "\r\n";
}
}
else { $debug = $debug.'<br/>Empty result'; /*DEBUG*/ }
}
else { $debug = $debug.'<br/>Oups, Query error!<br/>Query: '.$query_exp.'<br/>Error: '.$mysqli->error.'.'; /*DEBUG*/ }
require 'classs.php';
}
export('SELECT * FROM exel;','file');
答
连接后,你需要告诉您要发送的MySQL,并希望得到UTF8:
$mysql->query("set names 'utf-8'");
同样的问题,我加入像这样$库MySQLi =新的mysqli( 'localhost','root','','test'); if($ mysqli-> connect_errno) { $ debug = $ debug。'
无法连接到MySQL:('。$ mysqli-> connect_errno。')'。 $ mysqli-> connect_error; } else { $ debug = $ debug。'
连接到'。 $ mysqli-> host_info; } $ mysqli-> query(“set names'utf-8'”);它显示出像ط¹ظ“ظٹ – choppermio 2015-02-07 12:58:29