Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nasirkhan's avatar

Export large Excel/CSV (10-20K records)

I need to export Excel (CSV is ok) where will be about 10-20K records. I tried to export the package Maatwebsite/Laravel-Excel ( https://github.com/Maatwebsite/Laravel-Excel ). it works perfectly for small amount of records but does not work for this large files.

what could be the way around of this situation?

0 likes
5 replies
ohffs's avatar

Maybe try spout - it's a lot lighter than the php excel. It doesn't do everything that php excel does, but for straight xls it's pretty fast.

KenoKokoro's avatar

Make chunks of the files. I have been using this to export over 100k rows from database into excel files

const MAX_CHUNK_ROWS = 2500;

...

$backupRowsCount = count($this->exportData)
$current = 0;
while ($current < $backupRowsCount) {
            $next = $current + self::MAX_CHUNK_ROWS;
            if (is_array($this->exportData)) {
                $chunk = array_slice($this->exportData, $current, self::MAX_CHUNK_ROWS, true);
            }
            else {
               $chunk = $this->exportData->slice($current,self::MAX_CHUNK_ROWS, true);
            }
            $command->exportName .= "{$current}_{$next}";
            $command->exportData = $chunk;
            $command->exportFile();
            $current += self::MAX_CHUNK_ROWS;
            $percent = ($next / $backupRowsCount) * 100;
            $this->comment("{$next} rows exported to {$command->exportName} [{$percent}%]");
            $command->exportName = $oldExportName;
        }

Please or to participate in this conversation.