doudi8298 2012-06-27 09:28
浏览 72

导出为CSV时,俄语字符将不会显示

I have this code which I use to export a query in CSV, the problem is that, if I open this with Excel the russian characters won't display, but if I open it with numbers(Mac) they display.

Now, I can't get what's wrong with this. I've added some lines I saw on internet and nothing..

<?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('', '', '') or die(mysql_error());
mysql_select_db('', $conn) or die(mysql_error($conn));
mysql_query("SET NAMES UTF8");

/*
 * execute sql query
 */

$query = sprintf('SELECT fields FROM 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; charset=utf-8');
header('Content-Disposition: attachment;filename=hostess.csv');

/*
 * 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('/\|\
|,|"/', $field)) {
            $field = '"' . str_replace('"', '""', $field) . '"';
        }
        echo $separator . $field;
        $separator = ',';
    }
    echo "
";
}
?>
  • 写回答

1条回答 默认 最新

  • dongsheng1238 2012-08-23 17:25
    关注

    The script was not created for UTF-8 encoded data. It dumps the data almost as-is. The resulting CSV file will contain (probably) valid UTF-8 encoded data but no signature. In the absence of signature, some software will use heuristics to detect the encoding; others, like Excel, won't.

    You must tell excel to treat the file as UTF-8 encoded. For this, you need to import the file in Excel (Data > Get External Data > From Text) instead of opening it directly (double-clicking or using File > Open). Inside the Text Import Wizard, choose the appropriate encoding and Excel should import the data correctly. See screenshots below.

    Alternately, you could try adding the UTF-8 signature manually. I haven't tried it myself.

    // ...
    header('Content-Disposition: attachment;filename=hostess.csv');
    echo "\xEF\xBB\xBF";
    // ...
    

    This is what you see if you open the file directlyThis is what you see if you choose correct encoding

    评论

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集