银河源码

高品质源码点击这里给我发消息PHP-LotusAdmin官方论坛

分享一个PHP高性能导出excel的好方法

为什么说高性能?

使用过phpoffice系列,也就是phpexcel这个非常著名的扩展,但是但是,导出1000条以上的数据表,显得非常吃力,尝试使用php的cli模式,放到服务端导出,又发现一个问题,内存泄漏,并且在执行几次后内存超过.于是自己写了个方法,导出excel,性能远超phpexcel插件

原理是什么?

简单粗暴,组装html成xls,比phpexcel内部内处理要快5倍左右,支持大数据量的导出,具体根据你数据量和请求时间的多少而定.

//控制器内使用,组装列表
//$list反正就是个数组列表
$query = $model->find();
$list  = $query->asArray()->all();
//列标题
$header = ['登录名','昵称','角色','级别','状态','最后登录时间','创建时间','交易账号','姓名','消息开关状态','用户积分','房间名称','房间ID','是否VIP'];
$datas = [];
foreach ($list as $k => $v) {
    // $excel->HandleLarge($times);
    $datas[$k]['登录名'] = $v['user_loginname'];
    $datas[$k]['昵称'] = $v['user_nickname'];
}
$name = '用户导出';
$this->ExcelPull($name, $header, $datas);

    
    //XLS导出
    /**
    $name  string 文件名称
    $header array 列标题
    $dataResult  数组
    **/
    public  function ExcelPull($name, $header, $dataResult)
    {
        //这一行没啥用,根据具体情况优化下
        $headTitle = "xx详情";
        $headtitle= "<tr style='height:50px;border-style:none;><td border=\"0\" style='height:90px;width:470px;font-size:22px;' colspan='11' >{$headTitle}</th></tr>";
        $titlename = "<tr>";
        foreach ($header as $v) {
            $titlename .= "<td>$v</td>";
        }
        $titlename .= "</tr>";
        $fileName = date("Y-m-d") . "-" . $name . ".xls";
        $this->excelData($dataResult, $titlename, $headtitle, $fileName);
    }


   public  function excelData($data, $titlename, $title, $filename)
    {
        $str = "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\nxmlns:x=\"urn:schemas-microsoft-com:office:excel\"\r\nxmlns=\"http://www.w3.org/TR/REC-html40\">\r\n<head>\r\n<meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">\r\n</head>\r\n<body>";
        $str .="<table border=1>" . $titlename;
        $str .= '';
        foreach ($data as $key => $rt) {
            $str .= "<tr>";
            foreach ($rt as $v) {
                $str .= "<td >{$v}</td>";
            }
            $str .= "</tr>\n";
        }
        $str .= "</table></body></html>";
        $str .= "<span>creator:".yii::$app->user->identity->user_loginname."</span>";
        header("Content-Type: application/vnd.ms-excel; name='excel'");
        header("Content-type: application/octet-stream");
        header("Content-Disposition: attachment; filename=" . $filename);
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Pragma: no-cache");
        header("Expires: 0");
        exit($str);
    }

这是一张封面图,没啥用.请忽略

如果支付后未自动下载,请主动联系站长,站长QQ 610176732

当前页面:银河源码 » 分享一个PHP高性能导出excel的好方法

评论 538

文章评论已关闭!

  1. Cramp ttn.nosy.waytomilky.com.mcb.qd soon, [URL=http://dockpartsusa.com/payday-loans-ogden-utah/ - payday loans boise[/URL - payday loans http://dockpartsusa.com/payday-loans-ogden-utah/#payday-loans-ogden-utah loans for people with no credit [URL=http://golfeatoncanyongc.com/personal-loans-in-charleston-sc/ - secured personal loans[/URL - easy personal loans online http://golfeatoncanyongc.com/personal-loans-in-charleston-sc/#easy-personal-loans-online personal loans personal loans phoenix az [URL=http://websolutionsdone.com/synthroid/ - synthroid[/URL - thyroxine tablets http://websolutionsdone.com/synthroid/#levothyroxine-supplementation synthroid 100 [URL=http://dockpartsusa.com/online-loans/ - payday loans online alberta[/URL - online loans http://dockpartsusa.com/online-loans/#payday-loans-online-ohio online payday loans texas [URL=http://scoutcampreviews.com/buy-lasix-online/ - lasix without prescription[/URL - lasix no prescription http://scoutcampreviews.com/buy-lasix-online/#order-lasix-online lasix [URL=http://palawan-resorts.com/generic-cialis-lowest-price/ - cheapest price for cialis[/URL - canadian pharmacy cialis 20mg http://palawan-resorts.com/generic-cialis-lowest-price/#cialis canadian pharmacy cialis 20mg sobre cialis [URL=http://downtownrichmondassociation.com/generic-levitra/ - levitra cheap[/URL - low cost levitra 20 mg http://downtownrichmondassociation.com/generic-levitra/#generic-levitra buy levitra 20mg [URL=http://golfeatoncanyongc.com/payday-loans-near-me/ - payday loans near me[/URL - payday loans http://golfeatoncanyongc.com/payday-loans-near-me/#las-vegas-payday-loans bad credit payday loans direct lenders only delirium, nephrocalcinosis; studied?

    ewufokey 2019-03-09    
  2. Sources myx.hswu.waytomilky.com.tam.rx nystagmus premeds mosque, [URL=http://clearcandybags.com/viagra-100mg/ - viagra online[/URL - viagra online http://clearcandybags.com/viagra-100mg/#viagra-100mg sildenafil 100 m.g. [URL=https://pharm24rx.wixsite.com/lasix/ - lasix on internet[/URL - lasix online https://pharm24rx.wixsite.com/lasix/#lasix lasix to buy online no prescription [URL=http://myinxus.com/levitra-20mg/ - cheap levitra[/URL - levitra male enhancement http://myinxus.com/levitra-20mg/#buy-levitra-online generic levitra 20mg levitra [URL=http://palawan-resorts.com/100-mg-viagra-lowest-price/ - viagra[/URL - www.viagra.com http://palawan-resorts.com/100-mg-viagra-lowest-price/#100-mg-viagra-lowest-price viagra [URL=http://palawan-resorts.com/amoxicillin/ - amoxicillin without prescription[/URL - buy amoxil onlinr http://palawan-resorts.com/amoxicillin/#amoxil-for-cats amoxicillin buy [URL=http://myinxus.com/cialis-generic/ - order cialis or viasgra from canada[/URL - cialis coupon http://myinxus.com/cialis-generic/#tadalafil-20mg cialis side-opening personas, radiologist.

    cabiyavigebgu 2019-03-09    
  3. God, bgu.zwnx.waytomilky.com.qbk.gr phase-contrast [URL=http://scoutcampreviews.com/levitra-20mg/ - levitra[/URL - assunzione levitra http://scoutcampreviews.com/levitra-20mg/#levitra-20mg levitra buy online [URL=http://promexicoglobal.com/cash-loans/ - online loans for bad credit no credit check[/URL - fast cash loans http://promexicoglobal.com/cash-loans/#online-loans-for-bad-credit-no-credit-check cash loans what do you need to get a payday loan [URL=http://downtownrichmondassociation.com/tadalafil/ - tadalafil[/URL - cialis coupon 20 mg http://downtownrichmondassociation.com/tadalafil/#canada-cialis lowest price generic cialis [URL=http://palawan-resorts.com/amoxicillin/ - buy amoxicillin without prescription uk[/URL - amoxicillin http://palawan-resorts.com/amoxicillin/#amoxicillin amoxicillin online [URL=http://dockpartsusa.com/online-cash-advance/ - check cash advance[/URL - cash advance http://dockpartsusa.com/online-cash-advance/#cash-advance online cash advance [URL=http://memoiselle.com/cash-loans/ - payday loans minneapolis[/URL - fast cash loans http://memoiselle.com/cash-loans/#fast-cash-loans cash loans [URL=http://myinxus.com/propecia/ - propecia generic[/URL - propecia http://myinxus.com/propecia/#buy-propecia-online propecia biochemical, metatarsophalangeal attend, location.

    oawucisenaben 2019-03-09    
  4. Menses: cec.pfgv.waytomilky.com.lly.sl corneal [URL=http://postconsumerlife.com/fast-easy-cash-loans/ - easy fast cash loans[/URL - fast easy cash loans http://postconsumerlife.com/fast-easy-cash-loans/#fast-cash-loans-today fast cash loans today [URL=http://dockpartsusa.com/cash-loans/ - cash loans edmonton[/URL - cash loans houston http://dockpartsusa.com/cash-loans/#cash-loans-edmonton cash fast loans [URL=http://ossoccer.org/approved-cash-advance/ - quick cash advance[/URL - cash advance loans online http://ossoccer.org/approved-cash-advance/#how-to-apply-for-a-personal-loan cash advance cash advance loans online [URL=http://anger-management-action.com/cash-loans/ - payday loan online[/URL - first time loans http://anger-management-action.com/cash-loans/#cash-fast-loans cash loans edmonton [URL=http://myinxus.com/viagra/ - nu viagra[/URL - viagra http://myinxus.com/viagra/#viagra-in-drug-test gen viagra cheapest viagra 100mg [URL=http://refrigeratordealers.com/buy-prednisone/ - prednisone without a prescription[/URL - order prednisone http://refrigeratordealers.com/buy-prednisone/#prednisone prednisone [URL=http://golfeatoncanyongc.com/payday-loans/ - houston payday loans[/URL - payday loans http://golfeatoncanyongc.com/payday-loans/#small-loan-companies payday loans online payday loans texas [URL=http://promexicoglobal.com/cash-advance/ - short term cash advance[/URL - pay day cash advance http://promexicoglobal.com/cash-advance/#cash-loans-with-bad-credit small payday loan prevented heavy efficient leukopenia.

    uduvelevaci 2019-03-09    
  5. buy tadalafil

    JaneHiest 2019-03-09    
  6. Unilateral gcq.vvac.waytomilky.com.mqh.qa clustering hundreds series [URL=http://gasmaskedlestat.com/pharmacy/ - canadian pharmacy online no script[/URL - pharmacy http://gasmaskedlestat.com/pharmacy/#canadian-cialis-pharmacy pharmacy [URL=http://vmwaredevotee.com/target-payday-loans/ - payday loans st louis mo[/URL - target payday loans http://vmwaredevotee.com/target-payday-loans/#payday-loans-loveland-co payday loans lewisville tx [URL=http://gasmaskedlestat.com/buy-prednisone-online/ - prednisone online no prescription[/URL - prednisone http://gasmaskedlestat.com/buy-prednisone-online/#prednisone-without-dr-prescription-usa online prednisone [URL=http://umichicago.com/payday-loans-direct-lender/ - payday loans las vegas nv[/URL - quick payday loans http://umichicago.com/payday-loans-direct-lender/#online-payday-loans-las-vegas quick money loan [URL=http://vmwaredevotee.com/cash-advance/ - cash advance online no credit check[/URL - payday cash advance http://vmwaredevotee.com/cash-advance/#cash-advance-stores best cash advance loans [URL=http://refrigeratordealers.com/prednisone/ - prednisone with no prescription[/URL - prednisone http://refrigeratordealers.com/prednisone/#prednisone-online-without-prescription prednisone with no prescription [URL=http://openastorenow.com/personal-loans-columbia-sc/ - best rate personal loan[/URL - personal loans in houston tx http://openastorenow.com/personal-loans-columbia-sc/#personal-loans-columbia-sc personal loans san antonio tx [URL=http://listigator.com/prednisone/ - prednisone 20 mg side effects[/URL - prednisone 20 mg side effects http://listigator.com/prednisone/#buy-prednisone-online-without-prescription buying prednisone online consolidation, singlehanded continuity.

    utoewosewuyao 2019-03-09    
  7. Start dky.odjw.waytomilky.com.gas.cw polycythaemia, coordinators, locking [URL=http://myinxus.com/viagra/ - dicloxacil viagra interaction[/URL - free viagra http://myinxus.com/viagra/#viagra-canada canadian pharmacy viagra brand [URL=http://listigator.com/cialis-pills/ - cialis[/URL - lowest price on cialis http://listigator.com/cialis-pills/#prezzi-cialis-generico patent cialis [URL=http://sci-ed.org/ventolin/ - ventolin inhaler[/URL - ventolin inhaler http://sci-ed.org/ventolin/#ventolin-inhaler albuterol ventolin [URL=http://palawan-resorts.com/cialis-com/ - cialis[/URL - cialis http://palawan-resorts.com/cialis-com/#cialis.com cialis [URL=http://sci-ed.org/levitra/ - levitra wholesale no prescription[/URL - genericlevitra20mg http://sci-ed.org/levitra/#buy-levitra cheap levitra [URL=http://scoutcampreviews.com/zithromax/ - buy zithromax[/URL - zithromax z-pak http://scoutcampreviews.com/zithromax/#azithromycin-zpack buy azithromycin online overnight lift ketotic trimethoprim lavage.

    iheradutlasik 2019-03-09