超级好用的excel导出方法,比phpexcel快n倍,并且无乱码

一般用phpexcel导出海量数据时非常慢,而且动不动就内存溢出,楼主当时也是苦不堪言啊,于是乎在网上翻来翻去终于功夫不负苦心人,让我找到了,修改了下在这里贴出来,方便大家,还是老话,废话不多说直接怼代码
/**
     * @creator Jimmy
     * @data 2018/1/05
     * @desc 数据导出到excel(csv文件)
     * @param $filename 导出的csv文件名称 如date("Y年m月j日").'-test.csv'
     * @param array $tileArray 所有列名称
     * @param array $dataArray 所有列数据
     */
    public  function exportToExcel($filename, $tileArray=[], $dataArray=[]){
        ini_set('memory_limit','512M');
        ini_set('max_execution_time',0);
        ob_end_clean();
        ob_start();
        header("Content-Type: text/csv");
        header("Content-Disposition:filename=".$filename);
        $fp=fopen('php://output','w');
        fwrite($fp, chr(0xEF).chr(0xBB).chr(0xBF));//转码 防止乱码(比如微信昵称(乱七八糟的))
        fputcsv($fp,$tileArray);
        $index = 0;
        foreach ($dataArray as $item) {
            if($index==1000){
                $index=0;
                ob_flush();
                flush();
            }
            $index++;
            fputcsv($fp,$item);
        }

        ob_flush();
        flush();
        ob_end_clean();
    }

这里是调用 用的是tp3.2.3

	    $ssssd = [1=>'正常 未兑换',2=>'正常 已兑换',3=>'正常 过期',4=>'正常商家已提现',5=>'作废'];
            $count      = $res->alias('l')->where($where)->count();// 查询满足要求的总记录数

            $Page       = new \Think\Page($count,10000);// 实例化分页类 传入总记录数和每页显示的记录数(25)
            $ppp = ceil($count/10000);
            $pp = range(1,$ppp);
            foreach ($pp as $kkk => $vvv) {
                $rs[$kkk] = $res->field('l.id lid,l.user_id,l.shop_id,l.code_num,l.code_src,l.winning_time,l.exchange_time,l.status,l.item_id iid,i.title,i.money,s.coder,u.nickname,u.head,s.manager,s.phone')->alias('l')->where($where)->join('sd_item i ON i.id=l.item_id')->join('left join  sd_shop s ON s.id=l.shop_id')->join('sd_user u ON u.id=l.user_id')->page($vvv.', 10000')->select();
                
                $str[$kkk] = "用户昵称,兑奖门店号,核销员,核销员手机号,中奖时间,兑换时间,奖项名称,中奖金额,中奖码,兑换状态";
                    
                $exl11[$kkk] = explode(',',$str[$kkk]);
                foreach ($rs[$kkk] as $k => $v){
          
                    if (!$v['nickname']) $v['nickname']           = '暂无数据';
                    if (!$v['coder']) $v['coder']                 = '暂无数据';
                    if (!$v['manager']) $v['manager']             = '暂无数据';
                    if (!$v['phone']) $v['phone']                 = '暂无数据';
                    if (!$v['winning_time']) $v['winning_time']   = '暂无数据';
                    if (!$v['exchange_time']) $v['exchange_time'] = '暂无数据';
                    if (!$v['title']) $v['title']                 = '暂无数据';
                    if (!$v['money']) $v['money']                 = '暂无数据';
                   
                    $exl[$kkk][] = array(
                        $v['nickname'],$v['coder'],$v['manager'],$v['phone'],$v['winning_time'],$v['exchange_time'],$v['title'],$v['money'],$ssssd[$v['status']]
                    );

                }

                $this->exportToExcel('兑奖记录_'.time().$vvv.'.csv',$exl11[$kkk],$exl[$kkk]);
            }
            exit();



你可能感兴趣的:(超级好用的excel导出方法,比phpexcel快n倍,并且无乱码)