PHP导出大量数据到excel表格

 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     }

 

posted @ 2016-10-13 14:33  流火行者  阅读(3027)  评论(0编辑  收藏  举报