数据量超过10万,用php_excel导出很慢而且数据导入不全,还占空间,用csv格式导出,很快还不出错.
set_time_limit(0);}
导出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');