Heim  >  Artikel  >  Backend-Entwicklung  >  操作系统 - php csv导出 在window下office打开正常,但在MAC系统下打开只有一列

操作系统 - php csv导出 在window下office打开正常,但在MAC系统下打开只有一列

WBOY
WBOYOriginal
2016-06-06 20:35:171419Durchsuche

csv导出代码:

public function export_csv(){
        if(($id = $this->get_input('resultId')) == '') {
            $this->msg('参数有误');
        }
        $result = call_mod_func('admin/activity_mod', 'get_result', array($id));
        $str = "姓名,邮箱,手机,微信号" . PHP_EOL;
        $str = iconv('utf-8', 'gb2312', $str);  //中文转码

        foreach($result['rows'] as $row) {
            $name = iconv('utf-8', 'gb2312', $row['name']);
            $email = $row['email'];
            $mobile = $row['mobile'];
            $weixinid = iconv('utf-8', 'gb2312', $row['weixinid']);

            $str .= $name . ',' . $email . ',' . $mobile . ',' . $weixinid . PHP_EOL;
        }

        $filename = date('Ymd', gettime()) . '.csv';  //设置文件名
        
        header("Content-type:text/csv");
        header("Content-Disposition:attachment;filename=".$filename);
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
        header('Expires:0');
        header('Pragma:public');
        echo $str;
    }

这段代码
苹果系统下,用excel打开只有一列:
操作系统 - php csv导出 在window下office打开正常,但在MAC系统下打开只有一列

windows系统下,用excel打开有4列:
操作系统 - php csv导出 在window下office打开正常,但在MAC系统下打开只有一列

请问这代码要怎么修改?才能兼容多个系统呢?
【如果用分号分隔的话,就MAC系统下正常,windows下只有一列】

回复内容:

csv导出代码:

public function export_csv(){
        if(($id = $this->get_input('resultId')) == '') {
            $this->msg('参数有误');
        }
        $result = call_mod_func('admin/activity_mod', 'get_result', array($id));
        $str = "姓名,邮箱,手机,微信号" . PHP_EOL;
        $str = iconv('utf-8', 'gb2312', $str);  //中文转码

        foreach($result['rows'] as $row) {
            $name = iconv('utf-8', 'gb2312', $row['name']);
            $email = $row['email'];
            $mobile = $row['mobile'];
            $weixinid = iconv('utf-8', 'gb2312', $row['weixinid']);

            $str .= $name . ',' . $email . ',' . $mobile . ',' . $weixinid . PHP_EOL;
        }

        $filename = date('Ymd', gettime()) . '.csv';  //设置文件名
        
        header("Content-type:text/csv");
        header("Content-Disposition:attachment;filename=".$filename);
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
        header('Expires:0');
        header('Pragma:public');
        echo $str;
    }

这段代码
苹果系统下,用excel打开只有一列:
操作系统 - php csv导出 在window下office打开正常,但在MAC系统下打开只有一列

windows系统下,用excel打开有4列:
操作系统 - php csv导出 在window下office打开正常,但在MAC系统下打开只有一列

请问这代码要怎么修改?才能兼容多个系统呢?
【如果用分号分隔的话,就MAC系统下正常,windows下只有一列】

这问题很难?竟然没人回答。。。。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn