银河源码

高品质源码点击这里给我发消息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. V, xkc.jlhd.waytomilky.com.rid.mq involute ablation, [URL=http://myinxus.com/viagra-online/ - generic viagra samples[/URL - viagra guaranteed pharmacy http://myinxus.com/viagra-online/#viagra-generic-100mg cheap cialis and viagra [URL=http://wyovacationrental.com/cialis-canada/ - cialis coupon[/URL - cialis 20 mg coupons http://wyovacationrental.com/cialis-canada/#cialis-coupon cialis canada [URL=http://listigator.com/cialis/ - cialis en europe[/URL - cialis 10mg http://listigator.com/cialis/#cialis-online-no-prescription cialis [URL=http://sci-ed.org/lasix/ - online lasix[/URL - lasix long http://sci-ed.org/lasix/#lasix-online online lasix lasix online no prescription [URL=http://websolutionsdone.com/generic-cialis/ - cialis generic[/URL - cialis generic http://websolutionsdone.com/generic-cialis/#buy-cialis-online buy cialis [URL=http://palawan-resorts.com/levitra-20-mg/ - levitra 20mg[/URL - levitra 20 mg http://palawan-resorts.com/levitra-20-mg/#generic-levitra buy levitra online outcome: debriefing incite prealbumin.

    ebasuvod 2019-03-05    
  2. furosemide 20 mg

    JudyHiest 2019-03-05    
  3. I ordered human hair wigs https://www.youtube.com/watch?v=ovU7TuvxKA8 for my friend for Christmas. I just recomend them

    human hair wigs 2019-02-14    
  4. 这个应该没有办法设置单元格属性吧,遇到身份证号这样的数据,直接被转换成科学计数,而不是字符串。

    菜头 2018-12-02    
    • 处理的时候,转成字符串就可以了.

      闻海南 2018-12-10