laravel分批导出大量数据excel

    /**
     * 构建csv数据
     * @param $export_list
     * @param $member_types
     * @return \Generator
     */
    public function buildCsvData($export_list,$member_types)
    {
        foreach ($export_list as $k => $item) {
            // 会员类型
            $item_member_type_id = $item->member_type_id;
            $cell = [];
            $cell[] = $item->id;
            $cell[] = optional(optional($item->member_address)->district)->path_name;
            $cell[] = isset($member_types[$item_member_type_id]) ? $member_types[$item_member_type_id]['name'] : "--";
            $cell[] = $item->company_name;
            $cell[] = $item->username;
            $cell[] = $item->mobile;
            $cell[] = $item->login_times;
            if ($item->qualification_auth == 1) {
                $cell[] = trans('member_company.qualification_auth.' . $item->qualification_auth);
            } else {
                $cell[] = trans('member_company.qualification_auth.' . ($item->qualification_auth ? $item->qualification_auth : 0));
            }
            $cell[] = $item->created_at;
            $cell[] = $item->last_login_time;
            $cell[] = optional($item->business_member)->realname;
//                $order_count = Order::where('member_id', $item->id)->where('status', Order::STATUS_200)->count();
            $order_count = count($item->order);
            $cell[] = $order_count;
            $cell[] = trans('member_company.first_camp_status_list.' . $item->first_camp_status);
            $cell[] = $item->first_camp_remark;
            yield $cell;  //使用生成器节省内存,适合计算大量的数据
            unset($export_list[$k]);
        }
    }

    /**
     * 导出scv格式的excel
     * @param $list
     * @param $member_types
     */
    public function exportCsv($list,$member_types)
    {
        set_time_limit(0);
        // 文件名称
        $fileName = '会员导出' . date("YmdHis");

        // 文件头
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.$fileName.'.csv"');
        header('Cache-Control: max-age=0');

        // 打开PHP文件句柄,php://output 表示直接输出到浏览器
        $fp = fopen('php://output', 'a');

        $header = [
            '会员编号', '区域', '会员类型', '公司名称', '用户名', '手机号', '登录次数', '认证状态', '注册时间', '最后登录时间', '业务员', '完成订单数', '首营状态', '首营备注'
        ];
        // 输出Excel列名信息
        foreach ($header as $key => $value) {
            // CSV的Excel支持GBK编码,一定要转换,否则乱码
            $header[$key] = iconv('utf-8', 'gbk', $value);
        }

        // 将数据通过fputcsv写到文件句柄
        fputcsv($fp, $header);

        // buffer size,刷新一下输出buffer,不要太大,也不要太小
        $bufferSize = 500;

        // 偏移量
        $offset = 0;

        // page size
        $limit = 2000;

        $list->with(['member_address', 'business_member', 'order' => function ($query) {
            $query->where('status', Order::STATUS_200);
        }])->chunk($limit, function ($export_list) use ($member_types,$fp,$bufferSize) {
            $data = $this->buildCsvData($export_list, $member_types);
            // 计数器
            $num = 0;
            // 实际数量
            $count = 0;
            foreach ($data as $k => $row) {
//                $num++;
//
                // 刷新一下输出buffer,防止由于数据过多造成问题
                if ($bufferSize == $num) {
                    ob_flush();
                    flush();
                    $num = 0;
                }

                // 数据格式转换
                foreach ($row as $key => $value) {
                    if (is_string($value)) {
                        $row[$key] = iconv('utf-8', 'gbk//IGNORE',"\t".$value);
                    }
                }

                fputcsv($fp, $row);
            }
        });
        fclose($fp);
        exit;
    }

zed
请先登录后发表评论
  • latest comments
  • 总共0条评论