Thinkphp将表格的数据导出到excel文件
Thinkphp将表格的数据导出到excel文件
------解决方案--------------------
<br />/**<br /> * 导出数据为excel表格<br /> [email protected] $data 一个二维数组,结构如同从数据库查出来的数组<br /> [email protected] $title excel的第一行标题,一个数组,如果为空则没有标题<br /> [email protected] $filename 下载的文件名<br /> [email 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 />