提供兩種導出excel方法 1 最簡單的導出excel header('Content-Type: application/vnd.ms-excel'); //設置文件類型 也可以將 vnd.ms-excel' 改成xml(導出xml文件) header('Content-Disposition: attachment;filename="雲平台用戶報表.xls"'); //設置導出的excel的名字 header('Cache-Control: max-age=0'); echo iconv("utf-8","gbk","時間\t錯誤碼\t出現次數\t錯誤碼含義\n"); // \t是制表符 \n是換行符 foreach ($arr as $key=>$val){ //$arr 是所要導出的數據 echo iconv("utf-8","gbk",date("Y-m-d",$val["time"])."\t".$val["error_code"]."\t".$val["num"]."\t".$val["code_mean"]."\n"); } 2.使用phpexcel導出excel 到網上下載phpexcel壓縮包 將phpexcel.php 和phpexcel壓縮包放入你的項目中 實例: require_once ('PHPExcel.php'); //引用這兩個文件 require_once ('PHPExcel/Writer/Excel2007.php'); //導出excel $objExcel = new PHPExcel(); $objProps = $objExcel->getProperties(); $objProps->setCreator("Zeal Li"); $objProps->setLastModifiedBy("Zeal Li"); $objProps->setTitle("Office XLS Test Document"); $objProps->setSubject("Office XLS Test Document, Demo"); $objProps->setDescription("Test document, generated by PHPExcel."); $objProps->setKeywords("office excel PHPExcel"); $objProps->setCategory("Test"); $objProps = $objExcel->getProperties(); $objProps->setCreator("Zeal Li"); $objProps->setLastModifiedBy("Zeal Li"); $objProps->setTitle("Office XLS Test Document"); $objProps->setSubject("Office XLS Test Document, Demo"); $objProps->setDescription("Test document, generated by PHPExcel."); $objProps->setKeywords("office excel PHPExcel"); $objProps->setCategory("Test"); //************************************* //設置當前的sheet索引,用於後續的內容操作。 //一般只有在使用多個sheet的時候才需要顯示調用。 //缺省情況下,PHPExcel會自動創建第一個sheet被設置SheetIndex=0 $objExcel->setActiveSheetIndex(0); $objActSheet = $objExcel->getActiveSheet(); //網游類 //設置當前活動sheet的名稱 $objActSheet->setTitle('網游類'); $objActSheet->setCellValue('A1','游戲名稱'); //生成的excel中第一個sheet裡面的第一行數據 $objActSheet->setCellValue('B1','具體域名'); $objActSheet->setCellValue('C1','服務器描述'); $objActSheet->setCellValue('D1','線路屬性'); $objActSheet->setCellValue('E1','提交的服務器IP'); $objActSheet->setCellValue('F1','對應優化路由條目'); $objActSheet->setCellValue('G1','關鍵字優化'); $objActSheet->setCellValue('H1','優化線路'); $objActSheet->setCellValue('I1','優化狀態'); foreach($result_webgame as $k1=>$v1){ //遍歷從數據庫中讀取的數據 $objActSheet->setCellValue('A'.($k1+2),$v1["gamename"]); $objActSheet->setCellValue('B'.($k1+2),$v1['domain']); $objActSheet->setCellValue('C'.($k1+2),$v1['gameserver']); $objActSheet->setCellValue('D'.($k1+2),$v1['line_attribute']); $objActSheet->setCellValue('E'.($k1+2),$v1['server_ip']); $objActSheet->setCellValue('F'.($k1+2),$v1['optimize_route']); $objActSheet->setCellValue('G'.($k1+2),$v1['keyword']); $objActSheet->setCellValue('H'.($k1+2),$v1['line']); $objActSheet->setCellValue('I'.($k1+2),$v1['optimize_status']); } //網站類 //添加一個新的worksheet $objExcel->createSheet(); $reource_ready=$objExcel->getSheet(1)->setTitle('網站類'); $reource_ready->setCellValue('A1','網站名稱'); $reource_ready->setCellValue('B1','具體域名'); $reource_ready->setCellValue('C1','線路屬性'); $reource_ready->setCellValue('D1','IP網段'); $reource_ready->setCellValue('E1','優化線路'); $reource_ready->setCellValue('F1','優化狀態'); foreach($result_website as $k1=>$v2){ $reource_ready->setCellValue('A'.($k1+2),$v2["name"]); $reource_ready->setCellValue('B'.($k1+2),$v2['domain']); $reource_ready->setCellValue('C'.($k1+2),$v2['line_attribute']); $reource_ready->setCellValue('D'.($k1+2),$v2['ip']); $reource_ready->setCellValue('E'.($k1+2),$v2['line']); $reource_ready->setCellValue('F'.($k1+2),$v2['optimize_status']); } //以此類推 可以導出含有多個sheet的excel