- /**
- * Export data to excel table
- *@param $data A two-dimensional array, the structure is the same as the array found from the database
- *@param $title The title of the first row of excel, an array, if it is empty, there will be no title
- * @param $filename Downloaded file name
- *@examlpe
- $stu = M ('User');
- $arr = $stu -> select();
- exportexcel($arr,array('id','account ','Password','Nickname'),'File name!');
- */
- 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 "$titlen";
- }
- 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);
- }
- }
复制代码
|