还不错的PHP导出EXCEL函数挺好用的

直接上函数吧

    //导出 $data内容二维数组  $title各个标题  $filename表名称
    function exportexcelinfo($data=array(),$title=array(),$filename)
    {
        $encoded_filename = urlencode($filename);
         //header("Content-type:application/octet-stream");
        header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
        header("Accept-Ranges:bytes");
        header("Content-type:application/vnd.ms-excel");
        $ua = $_SERVER["HTTP_USER_AGENT"];
        if(preg_match("/MSIE/", $ua)) {
              header('Content-Disposition: attachment; filename="' . $encoded_filename . '.xls"');
        } else if (preg_match("/Firefox/", $ua)) {
              header('Content-Disposition: attachment; filename*="utf8\'\'' . $filename . '.xls"');
        } else {
              header('Content-Disposition: attachment; filename="' . $filename . '.xls"');
        }
        header("Pragma: no-cache");

        header("Expires: 0");
        //导出xls 开始
        if (!empty($title)){
            foreach ($title as $k => $v) {
                $title[$k]=iconv("UTF-8", "GB2312",$v);
                //$title[$k]=iconv("BIG5", "GB2312",$v);
            }
            $title= implode("\t", $title);
            echo "$title\n";
        }
        if (!empty($data)){
            foreach($data as $key=>$val){
                foreach ($val as $ck => $cv) {
                    //$data[$key][$ck]=iconv("UTF-8", "GB2312", $cv);
                    $data[$key][$ck]= mb_convert_encoding($cv, "GBK", "UTF-8");
                }
                $data[$key]=implode("\t", $data[$key]);

            }
            echo implode("\n",$data);
        }
    }

 

你可能感兴趣的:(还不错的PHP导出EXCEL函数挺好用的)