PHP 导出Excel三种方式 阿星小栈

public function getVideoExcelExport(){ $dataResult = Video::select(\'id\',\'title\',\'price_way\', \'playnum\',\'content\',\'video_type\')->get()->toArray(); foreach ($dataResult as $k=>$v){ //0免费 1VIP 2付费 $dataResult[$k][\'price_way_text\'] = ($v[\'price_way\'] == 0)?\'免费\':(($v[\'price_way\'] == 1)?\'VIP\':\'付费\'); $dataResult[$k][\'video_type_text\'] = MappingUtil::getTextByVideoType($v[\'video_type\']); } $headTitle = "视频记录"; $title = "视频记录"; $headtitle= "<tr style=\'height:50px;border-style:none;>\<th border=\"0\" style=\'height:60px;width:270px;font-size:22px;\' colspan=\'11\' >{$headTitle}</th></tr>"; $titlename = "<tr> <th style=\'width:70px;\' >ID</th> <th style=\'width:300px;\' >标题</th> <th style=\'width:70px;\'>付费模式</th> <th style=\'width:70px;\'>视频类型</th> <th style=\'width:600px;\'>内容</th> </tr>"; $filename = $title.".xls"; $this->excelData($dataResult,$titlename,$headtitle,$filename); } /*视频导出Excel */ public function excelData($datas,$titlename,$title,$filename){ $columns = [\'id\',\'title\',\'price_way_text\',\'video_type_text\',\'content\']; $str = "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\nxmlns:x=\"urn:schemas-microsoft-com:office:excel\"\r\nxmlns=\"http://www.w3.org/TR/REC-html40\">\r\n<head>\r\n<meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">\r\n</head>\r\n<body>"; $str .="<table border=1><head>".$titlename."</head>"; //$str .= $title; $dataResult = []; foreach ($datas as $key=>$value){ foreach ($columns as $k=>$v){ if (isset($datas[$key][$v])){ $dataResult[$key][$v] = $datas[$key][$v]; } } } foreach ($dataResult as $key=> $rt ){ $str .= "<tr>"; foreach ( $rt as $k => $v ) { $str .= "<td>{$v}</td>"; } $str .= "</tr>\n"; } $str .= "</table></body></html>"; header( "Content-Type: application/vnd.ms-excel; name=\'excel\'" ); header( "Content-type: application/octet-stream" ); header( "Content-Disposition: attachment; filename=".$filename ); header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" ); header( "Pragma: no-cache" ); header( "Expires: 0" ); exit( $str ); }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzjdxw.html