一、第一種實現的方法php
set_time_limit(0); //逐條導出數據 ob_end_clean(); header("Content-type: application/vnd.ms-excel"); header('Content-Disposition: attachment; filename="文章信息統計'.date('YmdHis').'.xls"'); $fp = fopen('php://output', 'w'); fwrite($fp, chr(0xEF).chr(0xBB).chr(0xBF)); $title = array('文章標題','平臺','名稱(id)','分類','發佈時間','文章位置'); fputcsv($fp, $title, "\t"); //查詢出要導出的內容 此處可根據本身要查詢的內容作修改 $carwlInfo = $crawlModel->setFields('*')->where($where)->order($order)->select(); $body = array(); foreach($carwlInfo as $key=>$val){ $body['title'] = $val['title']; $body['name'] = $val['name']; $body['type'] = $val['type']; $body['behotTime'] = $val['behotTime']; $body['position'] = $val['position']; fputcsv($fp, $body, "\t"); }
二、第二種實現的方法:app
//輸出的文件類型爲excel header("Content-type:application/vnd.ms-excel"); //提示下載 header("Content-Disposition:attachement;filename=Report_".date("Ymd").".xls"); //報表數據 $ReportArr = array( array('A','B','C','D','E'), array('文章id','文章標題','文章url','文章發佈時間','文章類似數'), ); $ReportContent = ''; $num1 = count($ReportArr); for ($i=0; $i<$num1; $i++) { $num2 = count($ReportArr[$i]); for ($j=0; $j<$num2; $j++) { //ecxel都是一格一格的,用\t將每一行的數據鏈接起來 $ReportContent .= '"'.$ReportArr[$i][$j].'"'."\t"; } //最後鏈接\n 表示換行 $ReportContent .= "\n"; } //用的utf-8 最後轉換一個編碼爲gb $ReportContent = mb_convert_encoding($ReportContent, "gb2312", "utf-8"); //輸出即提示下載 echo $ReportContent;