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

WBOY
Release: 2016-06-23 14:00:32
Original
1002 people have browsed it

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


回复讨论(解决方案)

/**	    * 导出数据为excel表格	    *@param $data    一个二维数组,结构如同从数据库查出来的数组	    *@param $title   excel的第一行标题,一个数组,如果为空则没有标题	    *@param $filename 下载的文件名	    *@examlpe 	    $stu = M ('User');	    $arr = $stu -> select();	    exportexcel($arr,array('id','账户','密码','昵称'),'文件名!');	*/	 function exportexcel($data=array(),$title=array(),$filename='report'){	    header("Content-type:application/octet-stream");	    header("Accept-Ranges:bytes");	    header("Content-type:application/vnd.ms-excel");  	    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= 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]=implode("\t", $data[$key]);	            	        }	        echo implode("\n",$data);	    }	 }
Copy after login
Copy after login

/**	    * 导出数据为excel表格	    *@param $data    一个二维数组,结构如同从数据库查出来的数组	    *@param $title   excel的第一行标题,一个数组,如果为空则没有标题	    *@param $filename 下载的文件名	    *@examlpe 	    $stu = M ('User');	    $arr = $stu -> select();	    exportexcel($arr,array('id','账户','密码','昵称'),'文件名!');	*/	 function exportexcel($data=array(),$title=array(),$filename='report'){	    header("Content-type:application/octet-stream");	    header("Accept-Ranges:bytes");	    header("Content-type:application/vnd.ms-excel");  	    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= 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]=implode("\t", $data[$key]);	            	        }	        echo implode("\n",$data);	    }	 }
Copy after login
Copy after login

$date $title $filename是前台页面传过来的值吗

你前台传过来的值 最好是经过后台的处理 再调用这个方法 传参数

学习了,不错

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!