Home  >  Article  >  Backend Development  >  PHP导出Excel函数

PHP导出Excel函数

WBOY
WBOYOriginal
2016-06-20 13:01:521434browse

<p>/**    </p><p>* 导出数据为excel表格    </p><p>*@param $data    一个二维数组,结构如同从数据库查出来的数组    </p><p>*@param $title   excel的第一行标题,一个数组,如果为空则没有标题    </p><p>*@param $filename 下载的文件名    </p><p>*@examlpe     $arr[] = array('12','13','14','15');    exportexcel($arr,array('id','账户','密码','昵称'),'文件名!');</p><p>*/ </p><p>function exportexcel($data=array(),$title=array(),$filename='report'){</p><p>    header("Content-type:application/octet-stream");</p><p>    header("Accept-Ranges:bytes");</p><p>    header("Content-type:application/vnd.ms-excel");</p><p>      header("Content-Disposition:attachment;filename=".$filename.".xls");</p><p>    header("Pragma: no-cache");</p><p>    header("Expires: 0");    //导出xls 开始</p><p>    if (!empty($title)){</p><p>        foreach ($title as $k => $v) {</p><p>            $title[$k]=iconv("UTF-8", "GB2312",$v);</p><p>        }</p><p>        $title= implode("\t", $title);</p><p>        echo "$title\n";</p><p>    }</p><p>    if (!empty($data)){</p><p>        foreach($data as $key=>$val){</p><p>            foreach ($val as $ck => $cv) {</p><p>                $data[$key][$ck]=iconv("UTF-8", "GB2312", $cv);</p><p>            }</p><p>            $data[$key]=implode("\t", $data[$key]);</p><p>                    }</p><p>        echo implode("\n",$data);</p><p>    } </p><p>}</p>


Statement:
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