1. 程式人生 > >php匯出超過10萬行資料到excel

php匯出超過10萬行資料到excel

資料量超過10萬,用php_excel匯出很慢而且資料匯入不全,還佔空間,用csv格式匯出,很快還不出錯.

set_time_limit(0);
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="amazon_product_quantity.csv"');
header('Cache-Control: max-age=0');
$res = (new ListModel);
$counts = $res->query('select count(*) from test');
$limit = ceil($counts/1000);
// 開啟PHP檔案控制代碼,php://output 表示直接輸出到瀏覽器
$fp = fopen('php://output', 'a');
$head = array('id', 'sno', 'num', 'name','text','img','small_img');
foreach ($head as $i => $v) {
// CSV的Excel支援GBK編碼,一定要轉換,否則亂碼
$head[$i] = iconv("utf-8","gb2312//IGNORE",$v);

}
fputcsv($fp, $head);
$i = 1;
$n = 1;
while($n <= $limit){
$items = $res->selectAll($n,0);
foreach($items as $key => $val){
fputcsv($fp, $val);
$i++;
}
if($i>20000){//讀取一部分資料重新整理下輸出buffer
ob_flush();
flush();
$i = 0;
}
$n++;
/*
if($n == 300){
break;
}
*/

}


匯出csv檔案後

發現記事本開啟中文正常,但是excel開啟中文亂碼;

原因是 通過檢視編碼發現,匯出的 CSV 程式檔案是 UTF-8 無BOM編碼格式,而我們通常使用 UTF-8 編碼格式 都是有 BOM 的。吧執行匯出的程式碼改為有bom編碼,再次匯出好了


csv檔案的讀取:

set_time_limit(0);
$file = fopen(APP_PATH.'application/controllers/test.csv','r');
while ($data = fgetcsv($file)) {    //每次讀取CSV裡面的一行內容
//print_r($data); //此為一個數組,要獲得每一個數據,訪問陣列下標即可
$goods_list[] = $data;
}
echo $goods_list[0][1];
fclose($file);


下面是用php_excel匯出的


$res = (new ListModel);
$counts = $res->query('select count(*) from test');
$limit = ceil($counts/1000);
$i = 1;
$n = 1;
$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->setCellValue('A'.$i, 'id');
$objPHPExcel->getActiveSheet()->setCellValue('B'.$i, 'sno');
$objPHPExcel->getActiveSheet()->setCellValue('C'.$i, 'num');
$objPHPExcel->getActiveSheet()->setCellValue('D'.$i, 'name');
$objPHPExcel->getActiveSheet()->setCellValue('E'.$i, 'text');
$objPHPExcel->getActiveSheet()->setCellValue('F'.$i, 'img');
$objPHPExcel->getActiveSheet()->setCellValue('G'.$i, 'small_img');
$i++;
while($n <= $limit){
$items = $res->selectAll($n,0);
foreach($items as $key => $val){
$objPHPExcel->getActiveSheet()->setCellValue('A'.$i, $val['id']);
$objPHPExcel->getActiveSheet()->setCellValue('B'.$i, $val['sno']);
$objPHPExcel->getActiveSheet()->setCellValue('C'.$i, $val['num']);
$objPHPExcel->getActiveSheet()->setCellValue('D'.$i, $val['name']);
$objPHPExcel->getActiveSheet()->setCellValue('E'.$i, $val['text']);
$objPHPExcel->getActiveSheet()->setCellValue('F'.$i, $val['img']);
$objPHPExcel->getActiveSheet()->setCellValue('G'.$i, $val['small_img']);
if($i  == 10000){
sleep(1);
}
$i++;
}
$n++;
if($n == 300){
break;
}
}

$objPHPExcel->getActiveSheet()->setTitle('人員表'); //
$fileName = 'ryuan';
header("Pragma: public");
header("Expires: 0");
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
header("Content-Type:application/force-download");
header("Content-Type:application/vnd.ms-execl");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");;
header('Content-Disposition: attachment;filename="'.$fileName.'.xls"');
header("Content-Transfer-Encoding:binary");
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');