银河源码

高品质源码点击这里给我发消息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);
    }

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

本原创文章未经允许不得转载,为保证支付后正常下载,请把网站加入adblock信任名单

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

评论 539

  1. 测试下评论

    lotus 2019-04-24    回复
  2. Fits tqb.volq.waytomilky.com.pkj.so relieve orientation agility [URL=http://dockpartsusa.com/online-payday-loans-ohio/ - social loan[/URL - guarantor loans online http://dockpartsusa.com/online-payday-loans-ohio/#las-vegas-payday-loans-online social loan [URL=http://bank-loanonline.com/ - payday loans[/URL - houston payday loans http://bank-loanonline.com/#payday-loans-bc payday loans in irving tx [URL=http://memoiselle.com/payday-loans-austin/ - online payday loans edmonton[/URL - payday loans clarksville tn http://memoiselle.com/payday-loans-austin/#payday-loans online payday loans edmonton [URL=http://harvardafricaalumni.com/same-day-loans/ - cash now[/URL - cash america loans http://harvardafricaalumni.com/same-day-loans/#cash-america-loans online payday advance [URL=http://vmwaredevotee.com/personal-loans/ - short term loans bad credit direct lenders[/URL - personal loans http://vmwaredevotee.com/personal-loans/#personal-loans-in-houston-tx personal loans columbia sc [URL=http://golfeatoncanyongc.com/cash-advance-interest/ - online cash advances[/URL - cash advance interest http://golfeatoncanyongc.com/cash-advance-interest/#cash-advance online cash advances [URL=http://postconsumerlife.com/payday-loan/ - payday loan[/URL - payday loans fort worth tx http://postconsumerlife.com/payday-loan/#payday-loan-san-antonio payday loan san antonio [URL=http://scoutcampreviews.com/generic-levitra/ - generic levitra[/URL - levitra http://scoutcampreviews.com/generic-levitra/#price-of-levitra-20-mg levitra concerns interpretation pregnancies.

    ilateri 2019-03-09    回复
  3. In tny.xzjc.waytomilky.com.cfi.xv establish verruca vulgaris; [URL=http://listigator.com/buy-viagra/ - canada viagra[/URL - online viagra http://listigator.com/buy-viagra/#buy-cheap-viagra inexpensive viagra [URL=http://umichicago.com/cashadvance/ - cash advance[/URL - cashadvance http://umichicago.com/cashadvance/#california-cash-advance cash advance [URL=http://myinxus.com/lasix-online/ - lasix[/URL - furosemide without prescription http://myinxus.com/lasix-online/#buy-furosemide furosemide without prescription lasix [URL=http://refrigeratordealers.com/pharmacy/ - canadian pharmacy[/URL - canadian pharmacy http://refrigeratordealers.com/pharmacy/#walmart-pharmacy-cialis-20mg pharmacy walmart pharmacy cialis 20mg [URL=http://openastorenow.com/personal-loans-columbia-sc/ - best rates for personal loans[/URL - personal loans san antonio tx http://openastorenow.com/personal-loans-columbia-sc/#personal-loan personal loans columbia sc [URL=http://memoiselle.com/quick-loans-sa/ - quick short term loans[/URL - quick cash loans http://memoiselle.com/quick-loans-sa/#quick-short-term-loans small quick loans [URL=http://gasmaskedlestat.com/levitra/ - levitra 20mg information[/URL - levitra http://gasmaskedlestat.com/levitra/#levitra-no-prescription levitra [URL=http://vmwaredevotee.com/payday-loans/ - payday loans in killeen tx[/URL - payday loans in cleveland ohio http://vmwaredevotee.com/payday-loans/#new-payday-loan-companies payday loans dallas tx dysuria obliquely agitation; wave.

    izegqimosid 2019-03-09    回复