如何使用 laravel excel 导出大量行

Abh*_*hri 4 laravel-excel laravel-8

我需要将大量行导出到 Excel。我正在使用Laravel-excel。我遵循了导出大型数据库的文档中给出的所有建议。

// routes/web.php
Route::post('/dashboard/export', [DashboardController::class, 'export'])->middleware('auth');

// app/Http/Controllers/DashboardController.php
class DashboardController extends Controller
{

    public function export(Request $request)
    {
        $filename = str::random(20).".xlsx";
        (new UsersExport($request->all()))->queue($filename, 'public')->chain([
            new NotifyUserOfCompletedExcelExport(request()->user(),$filename ),
        ]);
        
        return json_encode([
            'message' => "You will receive email with download link once export is complete."
        ]);
        
    }
}


// app/Exports/UsersExport.php
class UsersExport implements FromCollection, WithHeadings
{
    use Exportable;
    public function __construct($config)
    {
        
        $this->config = $config;
    }
    
    public function collection()
    {
        $mains = DB::table('mains')->select('name', 'email', 'designation', 'country', 'university', 'discipline', 'subject', 'school_or_department');
        


        //filter one
        $one = $this->config['filterOne']['column'];
        if ($one != null) {
            // dd($one);
            $mains = $mains->where($one, 'like', $this->config['filterOne']['value'] . "%");
        }

        $mains = $mains->offset($this->config['page'] * $this->config['num_entries_per_page']);
        $mains = $mains->limit($this->config['num_entries_per_page']);
        $mains = $mains->where('delete', 0);
        return $mains->get();
    }
    
    public function headings(): array
    {
        return [
            'NAME', 'EMAIL', 'DESIGNATION', 'COUNTRY', 'UNIVERSITY', 'DISCIPLINE', 'SUBJECT', 'SCHOOL_OR_DEPARTMENT'
        ];
    }
}
Run Code Online (Sandbox Code Playgroud)

显然您可以看到,我正在排队导出。我在终端中运行命令。

php artisan queue:work
Run Code Online (Sandbox Code Playgroud)

对于大量行(30000),它给出如下输出,然后queue:work退出。

[2021-11-12 05:13:56][151] Processing: Maatwebsite\Excel\Jobs\QueueExport
[2021-11-12 05:14:17][151] Processed:  Maatwebsite\Excel\Jobs\QueueExport
[2021-11-12 05:14:25][152] Processing: Maatwebsite\Excel\Jobs\AppendDataToSheet
[2021-11-12 05:14:39][152] Processed:  Maatwebsite\Excel\Jobs\AppendDataToSheet
.
.
.
.
[2021-11-12 05:17:48][167] Processed:  Maatwebsite\Excel\Jobs\AppendDataToSheet
Run Code Online (Sandbox Code Playgroud)

我考虑过更改 php.ini 文件中的 php 内存限制

memory_limit = 512M
Run Code Online (Sandbox Code Playgroud)

但它仍然不起作用。

对于少量行队列:工作正常工作(不退出),输出如下。

[2021-11-12 03:21:19][112] Processing: Maatwebsite\Excel\Jobs\QueueExport
[2021-11-12 03:21:22][112] Processed:  Maatwebsite\Excel\Jobs\QueueExport
[2021-11-12 03:21:24][113] Processing: Maatwebsite\Excel\Jobs\AppendDataToSheet
[2021-11-12 03:21:26][113] Processed:  Maatwebsite\Excel\Jobs\AppendDataToSheet
[2021-11-12 03:21:27][114] Processing: Maatwebsite\Excel\Jobs\AppendDataToSheet
[2021-11-12 03:21:28][114] Processed:  Maatwebsite\Excel\Jobs\AppendDataToSheet
[2021-11-12 03:21:29][115] Processing: Maatwebsite\Excel\Jobs\CloseSheet
[2021-11-12 03:21:30][115] Processed:  Maatwebsite\Excel\Jobs\CloseSheet
[2021-11-12 03:21:30][116] Processing: Maatwebsite\Excel\Jobs\StoreQueuedExport
[2021-11-12 03:21:31][116] Processed:  Maatwebsite\Excel\Jobs\StoreQueuedExport
[2021-11-12 03:21:31][117] Processing: App\Jobs\NotifyUserOfCompletedExcelExport
[2021-11-12 03:21:35][117] Processed:  App\Jobs\NotifyUserOfCompletedExcelExport

Run Code Online (Sandbox Code Playgroud)

我的问题是:-

-> 如何导出如此大量的行?

-> 我做错了什么?

-> 有没有其他方法可以达到同样的目的?

-> 我应该创建一个单独的服务来使用其他语言导出 Excel 中的数据吗?

-> 在生产前端服务器上进行如此繁重的处理是个好主意吗?

Gil*_*ett 5

查看队列的超时值。来自https://laravel.com/docs/8.x/queues#job-expirations-and-timeouts的文档:

“queue:work Artisan 命令公开了一个 --timeout 选项。如果作业的处理时间超过超时值指定的秒数,则处理该作业的工作程序将退出并出现错误。通常,工作程序将重新启动由服务器上配置的流程管理器自动执行。”

涉及较少行数的作业看起来会在超时值内完成。更长的则不然 - 就像 php.ini 和 max_execution 时间一样,如果作业花费太长时间,系统会担心它以某种方式损坏,并终止作业。