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

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

WBOY
Release: 2016-06-06 20:35:17
Original
1453 people have browsed it

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;
    }
Copy after login
Copy after login

这段代码
苹果系统下,用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;
    }
Copy after login
Copy after login

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

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

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

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template