1 /** 2 * @creator Jimmy 3 * @data 2016/8/22 4 * @desc 資料匯出到excel(csv檔案) 5 * @param $filename 匯出的csv檔名稱 如date("Y年m月j日").'-PB機構列表.csv' 6 * @param array $tileArray 所有列名稱 7 * @param array $dataArray 所有列資料 8 */ 9 public static function exportToExcel($filename, $tileArray=[], $dataArray=[]){ 10 ini_set('memory_limit','512M'); 11 ini_set('max_execution_time',0); 12 ob_end_clean(); 13 ob_start(); 14 header("Content-Type: text/csv"); 15 header("Content-Disposition:filename=".$filename); 16 $fp=fopen('php://output','w'); 17 fwrite($fp, chr(0xEF).chr(0xBB).chr(0xBF)); 18 // $fp=fopen('D://hello.csv','w'); 19 fputcsv($fp,$tileArray); 20 $index = 0; 21 foreach ($dataArray as $item) { 22 if($index==1000){ 23 $index=0; 24 ob_flush(); 25 flush(); 26 } 27 $index++; 28 fputcsv($fp,$item); 29 } 30 31 ob_flush(); 32 flush(); 33 ob_end_clean(); 34 }