viest / php-ext-xlswriter

🚀 PHP Extension for creating and reader XLSX files.
https://xlswriter.viest.me
BSD 2-Clause "Simplified" License
2.26k stars 234 forks source link

如何在闭包里使用xlswriter对象 #367

Closed zh7314 closed 3 years ago

zh7314 commented 3 years ago

        BrokerMemberData::where('id', '>', 0)->chunkById(10000, function ($list) use (&$textFile,&$t) {
            foreach ($list as $k => $v) {

                $textFile->insertText($k + 1, 0, $v->id);
                $textFile->insertText($k + 1, 1, $v->name);
                $textFile->insertText($k + 1, 2, $v->phone);
                $textFile->insertText($k + 1, 3, $v->source);
                $textFile->insertText($k + 1, 4, $v->province_name);
                $textFile->insertText($k + 1, 5, $v->city_name);
                $textFile->insertText($k + 1, 6, $v->data_time);
                $textFile->insertText($k + 1, 7, $v->user_id);
                $textFile->insertText($k + 1, 8, $v->user_nickname);
                $textFile->insertText($k + 1, 9, $v->register_time);
                $textFile->insertText($k + 1, 10, $v->up_user_id);
                $textFile->insertText($k + 1, 11, $v->store_name);
                $textFile->insertText($k + 1, 12, $v->is_pull_new);
                p($t++);
            }
        });

        $filePath = $textFile->output();```   

$textFile对象会在最后一个步幅的10000数据刷到$textFile对象,不清楚如何在闭包里任何使用$textFile对象
viest commented 3 years ago

请自行维护全局单元格行坐标状态,你的代码会重复写入前10000个单元格,因为$k永远以0开始。