Home >Backend Development >PHP Tutorial >Thinkphp将报表的数据导出到excel文件

Thinkphp将报表的数据导出到excel文件

WBOY
WBOYOriginal
2016-06-13 11:51:47778browse

Thinkphp将表格的数据导出到excel文件
Thinkphp将表格的数据导出到excel文件
------解决方案--------------------

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

------解决方案--------------------
学习了,不错

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