csv表格导出20万条数据:
function doExport()
{
set_time_limit(0);
ini_set ('memory_limit', '256M');
ob_start();
try {
$fp = fopen('php://output','wb');
} catch (\Exception $e) {
exit("error");
}
$item = array('序号','数据');
fputcsv($fp,$item);
$num = 1;
$for_num = 200000;
for ($d=1;$d<=$for_num;$d++) {
$item = array($num,"数据:".uniqid()."\t");
$num++;
fputcsv($fp,$item);
}
$fileName='数据表'.uniqid().'.csv';
fclose($fp);
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);
header("Content-Transfer-Encoding:binary");
ob_end_flush();
}
doExport();
常见问题:
1.使用office打开csv 显示乱码: 解决办法:使用wps 打开。 2.另存为xlsx 

|