Laravel-Excel
Laravel-Excel copied to clipboard
[Bug]: Run out of memory with certain excel files and toArray
Is the bug applicable and reproducable to the latest version of the package and hasn't it been reported before?
- [X] Yes, it's still reproducable
What version of Laravel Excel are you using?
3.1.40
What version of Laravel are you using?
8.83.16
What version of PHP are you using?
7.4.27
Describe your issue
See related issue on PhpSpreadsheet
When I use Excel::toArray
on an excel spreadsheet which has every cell formatted as Arial / 10, the method reads every column from A
to WVI
even though there are only two columns of data. This causes a massive runtime and eventually an exhaustion of memory from more than ~100 rows.
How can the issue be reproduced?
Send this file in form-data to a laravel endpoint:
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
class TestController extends Controller
{
public function import(Request $request)
{
$file = $request->file('file');
$array = Excel::toArray(null, $file);
dd($array);
}
}
produces
Symfony\Component\ErrorHandler\Error\FatalError: Allowed memory size of 536870912 bytes exhausted (tried to allocate 83886080 bytes) in file /vendor/maatwebsite/excel/src/Cache/MemoryCache.php on line 97
What should be the expected behaviour?
Should be able to import the data from A1:B201 hastily and without any memory issues.
You could try WithColumnLimit.
Other than that there isn't really anything we can do in this package, as the toArray logic is from PhpSpreadsheet
WithQuery is too will not help. Use https://github.com/rap2hpoutre/fast-excel you can use chunk for export and import
Could you not choose to use rangeToArray
instead as stated in this issue?
I have similar problem with file approximately 300K rows, 5MB and import to collection, even I chunk read the whole collection have almost 3GB in RAM nazwiska-meskie-w-polsce-stan-na-2019-04-15.xlsx .
This bug report has been automatically closed because it has not had recent activity. If this is still an active bug, please comment to reopen. Thank you for your contributions.