小滕的博客

小滕的技术点滴

PHP导出csv在window下打开乱码

3 weeks ago · 0 MIN READ

mac下打开导出的csv文件上是正常的,但是window打开csv是乱码的,搜索了下解决方案,需要在导出内容前面加上BOM头,也就是这个:

chr(0xEF) . chr(0xBB) . chr(0xBF)

这里给一个示例:

$data = [chr(0xEF) . chr(0xBB) . chr(0xBF) . "学生,分数,是否及格"];
        foreach ($orders as $order) {
            $score = $userScoreData[$order->user->id];
            $data[] = $order->user->nick_name . ',' . $score . ',' . ($score >= $paper->pass_score ? '是' : '否');
        }
        header('Content-Type: application/vnd.ms-excel;charset=utf-8');
        header("Content-Disposition:attachment;filename=" . date('YmdHis') . '.csv');
        header('Cache-Control: max-age=0');
        return implode("\n", $data);

小笔记。

···

xiao teng



备案号:皖ICP备14012032号-5