php 输出excel方法

 public function getExcelFormat($filename,$members)
    {
        header ( "Content-type: application/octet-stream" );
        //用来告诉浏览器,文件是可以当做附件被下载,下载后的文件名称为$file_name该变量的值。
        Header ( "Content-Disposition: attachment; filename=".$filename.".csv" );

        ob_start(); //打开缓冲区

        $fp = fopen('php://output','w');
        $bom =  chr(0xEF).chr(0xBB).chr(0xBF);
        fputcsv($fp,[$bom.'id','手机号','姓名','注册时间']);
        foreach ($members as $line)
        {
            fputcsv($fp,$line);
        }
        $len =  ob_get_length();
        header ( "Accept-Length: " . $len);
        $content = ob_get_clean();
        echo $content;
        fclose($fp);

    }

猜你喜欢

转载自blog.csdn.net/qq_16059847/article/details/88890514