public function exportgood(){
? ? ? ? set_time_limit(0);
? ? ? ? ignore_user_abort(true);
? ? ? ? ini_set('memory_limit', '1024M');
??????? //查詢出來的結(jié)果$data
? ? ? ? $arr_excelHeaderinfo = ['A'=>['35','商品ID','goods_id'],'B'=>['50','貨號(hào)','goods_sn'],'C'=>['25','sku名稱','sku_name'],'D'=>['30','sku編碼','sku_code'],'E'=>['30','品牌','brand_name'],'F'=>['30','第三方貨號(hào)','third_goods_number'],'G'=>['120','商品名稱','goods_name'],'H'=>['50','商品型號(hào)','goods_model'],'I'=>['30','商品分類','cat_name'],'J'=>['30','供貨商','suppliers_name'],'K'=>['35','庫存數(shù)量','total_stocks'],'L'=>['10','可用庫存','goods_number'],'M'=>['10','成本價(jià)','cost_price'],'N'=>['10','商品售價(jià)','shop_price'],'O'=>['25','市場價(jià)','market_price'],'P'=>['30','是否上下架','is_on_sale'],'q'=>['30','采購價(jià)','ammout'],'r'=>['30','含豆價(jià)','been_ammout']];
? ? ? ? $fileName = "運(yùn)營商品數(shù)據(jù)".date("Ymd",time());
? ? ? ? $this->publicExportCsv($arr_excelHeaderinfo,$data,$fileName);
? ? }.
public function publicExportCsv($arr_excelHeaderinfo,$arr,$fileName){
? ? ? ? header('Access-Control-Allow-Origin:*');//允許所有來源訪問
? ? ? ? $headArr = array();
? ? ? ? $fieldArr = array();
? ? ? ? foreach ($arr_excelHeaderinfo as $k => $v) {
? ? ? ? ? ? $headArr[]=$v[1];
? ? ? ? ? ? $fieldArr[]=$v[2];
? ? ? ? }
? ? ? ? $data = array();
? ? ? ? foreach ($arr as $key => $val) {
? ? ? ? ? ? foreach ($fieldArr as $dk => $dv) {
? ? ? ? ? ? ? ? if (in_array($dv, array('parent_order_sn','order_sn','add_time','pay_time','shipping_time','invoice_no','mobile','goods_sn','goods_sku_name','id_card_number','sku_code','goods_model','third_goods_number'))) {
? ? ? ? ? ? ? ? ? ? $data[$key][] = "\xEF\xBB\xBF".$val[$dv];
? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? $data[$key][] = $val[$dv];
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? // $data=array(array("1","2","3"),array("11","22","33"),array("111","222","333")); //模擬表格數(shù)據(jù)
? ? ? ? /**導(dǎo)出api**/
? ? ? ? ini_set('memory_limit','1024M'); //設(shè)置程序運(yùn)行的內(nèi)存
? ? ? ? ini_set('max_execution_time',0); //設(shè)置程序的執(zhí)行時(shí)間,0為無上限
? ? ? ? ob_end_clean();? //清除內(nèi)存
? ? ? ? ob_start();
? ? ? ? header("Content-Type: text/csv");
? ? ? ? header("Content-Disposition:filename=".$fileName.'.csv');
? ? ? ? $fp=fopen('php://output','w');
? ? ? ? fwrite($fp, chr(0xEF).chr(0xBB).chr(0xBF));
? ? ? ? fputcsv($fp,$headArr);
? ? ? ? $index = 0;
? ? ? ? foreach ($data as $item) {
? ? ? ? ? ? if($index==1000){ //每次寫入1000條數(shù)據(jù)清除內(nèi)存
? ? ? ? ? ? ? ? $index=0;
? ? ? ? ? ? ? ? ob_flush();//清除內(nèi)存
? ? ? ? ? ? ? ? flush();
? ? ? ? ? ? }
? ? ? ? ? ? $index++;
? ? ? ? ? ? fputcsv($fp,$item);
? ? ? ? }
? ? ? ? ob_flush();
? ? ? ? flush();
? ? ? ? ob_end_clean();
? ? ? ? exit();
? ? }
實(shí)測可以導(dǎo)出2W+條數(shù)據(jù),如果出現(xiàn)500請(qǐng)優(yōu)化你的sql語句