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

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分