php 使用Laravel Excel导出大量数据超时错误

pokxtpni  于 5个月前  发布在  PHP
关注(0)|答案(1)|浏览(90)

我必须使用Maatwebsite Laravel Excel 3.1包从数据库导出超过10k记录到Excel。它可以很好地处理少量数据(小于10k)。我在导出类中使用作业和块。但我得到了超时错误。我的限制是512M。

namespace App\Exports;

use App\Events\Download\DownloadComplete;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Contracts\Support\Responsable;
use Illuminate\Support\Arr;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithCustomChunkSize;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Events\AfterSheet;
use App\Traits\ReportDownloadTrait;
use Throwable;
use App\PreDownloadData;

class ReportExport implements FromQuery, WithHeadings, ShouldAutoSize, Responsable, WithEvents, WithTitle, WithMapping, WithCustomChunkSize, ShouldQueue
{
    use Exportable, ReportDownloadTrait;

    protected array $headings, $inputs;
    protected int $download_id;

    public $fileName = "report_download.xlsx";
    public $timeout = 1200; // 5 hour
    public $tries = 3;

    public function query()
    {
        return DownloadData::whereDownloadId($this->download_id)->whereNotNull('row');
    }

    public function map($data): array
    {
        return $data->row;
    }

    public function batchSize(): int
    {
        return 200;
    }

    public function chunkSize(): int
    {
        return 200;
    }

    public function headings(): array
    {
      [];
    }

字符串

xmd2e60i

xmd2e60i1#

调整您的查询方法,直接使用分块,而不依赖于导出类中的batchSize和chunkSize。

DownloadData::whereDownloadId($this->download_id)
    ->whereNotNull('row')
    ->chunk(200, function ($data) {
        foreach ($data as $row) {
            // Process each row of data here
            // For example, you might export the row to a CSV file
            fputcsv($file, $row->toArray());
        }
    });

字符串
在这个例子中,chunk方法一次从数据库中检索200行数据。对于每个chunk,它循环遍历数据行并将每行导出到CSV文件。$file变量将是您使用fopen函数打开的文件句柄资源。

相关问题