描述:在实际工作中,有时候我们需要导出大量数据到execl里面,可以参考分页方式导出,将每一页的数据放进php输出流里面 代码如下
<?php ini_set('memory_limit', '2048M'); header('Content-Description: File Transfer'); header('Content-Type: application/vnd.ms-excel'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); $filename ="111.xlsx"; header("Content-Disposition:filename={$filename}"); $fp = fopen("php://output",'a'); $title = ["id", "name", "age","aa","bb"]; $str = implode("\t",$title)."\n"; fwrite($fp,$str,strlen($str)); $bigData=[]; for($i=0;$i<1000000;$i++){ $tmp=[]; $tmp["id"]=1; $tmp["name"]="lisi"; $tmp["age"]=20; $tmp["aa"] = 20; $tmp["bb"] = 20; array_push($bigData,$tmp); } $result = array_chunk($bigData,5); for($i=0;$i<count($result);$i++){ $data = $result[$i]; for($j=0;$j<count($data);$j++){ $show=[]; $id = $data[$j]["id"]; $name = $data[$j]["name"]; $age = $data[$j]["age"]; $aa = $data[$j]["aa"]; $bb = $data[$j]["bb"]; array_push($show,$id); array_push($show, $name); array_push($show, $age); array_push($show, $aa); array_push($show, $bb); $strshow = implode("\t",$show)."\n"; fwrite($fp,$strshow); } file_put_contents("./1.txt",$i,FILE_APPEND); ob_flush(); flush(); } fclose($fp); exit; ?>