PHP csv导出office excel乱码问题解决

<?php
class Csv_export {
    function render($filename,$title,$data=[]){
        ob_end_clean();
        header("Content-Type: application/force-download");
        header("Content-type:text/csv;charset=utf-8");
        header("Content-Disposition:filename={$filename}.csv");
        ob_end_flush();
        if($title){
            foreach ($title as $k=>&$tv){
                $tv = $this->gbkUtf($tv); //标题转码
            }
            echo implode(',',$title)."\r";
        }
        if($data){
            foreach ($data as $v){
                foreach ($v as &$vv){
                    $vv = $this->gbkUtf($vv); //内容转码
                }
                echo implode(',',$v)."\r";
            }
        }
    }
    function gbkUtf($value){
        return $value ? iconv("UTF-8", "GB2312//IGNORE", $value) : '';
    }
}

测试WPS和excel打开无乱码

posted @ 2020-05-14 16:15  大智如蠢  阅读(476)  评论(0编辑  收藏  举报