使用excel读取csv文件中的阿拉伯语时出错

时间:2015-02-07 12:18:29

标签: php excel csv export-to-excel

我有这个代码将数据库表导出为csv,我就像这样 它应该看起来像这样的علي。ط¨ط§ط±ط§ط³。我不知道是什么问题。是因为编码还是什么。和我的表Collat​​ion是utf8_general_ci

$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');

1 个答案:

答案 0 :(得分:0)

连接后,你需要告诉你要发送的mysql,并希望收到utf8:

  $mysql->query("set names 'utf-8'");