php-xlswriter Excel操作

function write($filename)
    {
        $config = ['path' => '/code/logs/'];
        $excel = new \Vtiful\Kernel\Excel($config);
        $header = [
            '订单号',
            '业务类型',
            '公司id',
            '贷款银行',
            '贷款银行名字',
            '进单时间',
        ];

        $filePath = $excel->constMemory($filename)
            ->header($header);
        $bar = $this->output->createProgressBar(Trade::count('id') / 2000);
        $bar->setFormat("   %elapsed:6s%/%estimated:-6s%   内存消耗: %memory:6s%\n%current%/%max% [%bar%] %percent:3s%%");

//        // 读取测试文件
//        $data = $excel->openFile('tutorial.xlsx')
//            ->openSheet();
//        $d = [];
//        while ($row = $excel->nextRow()) {
//            $this->info(json_encode($row, JSON_UNESCAPED_UNICODE));
//            $bar->advance();
//            $d[] = $row;
//        }
//        $bar->finish();
        Trade::orderBy('created_at')->chunk(2000, function ($trades) use ($filePath, $bar) {
            foreach ($trades as $trade) {
                $data[] = [
                    $trade->id,
                    $trade->product_type,
                    $trade->company_id,
                    $trade->admin_user_id,
                    $trade->loan_bank_code,
                    $trade->loan_bank_name,
                    $trade->created_at,
                ];
            }
            $filePath->data($data);
            $bar->advance();
        });
        $bar->finish();
        $filePath->output();
    }
posted @ 2020-04-07 20:20  Sentiger  阅读(1702)  评论(0编辑  收藏  举报