Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading very large (more than 100MB) Excel files in PHP

I'm trying to read a larger than 100MB Excel file using PHPExcel but it crashes while loading the file. I don't need any styling. I tried using:

$objReader->setReadDataOnly(true);

but it still crashes.

Is there any efficient way to read this size of Excel file in PHP?

like image 578
Husnain Ali Avatar asked Oct 13 '15 06:10

Husnain Ali


2 Answers

Try Spout: https://github.com/box/spout.

This is a PHP library that was created to solve your problem (reading/writing large files). Here is why it works:

Other libraries keep a representation of the spreadsheet in memory which make them subject to out of memory errors. Using some caching strategies will help with these kind of errors but will affect performance pretty badly.

On the other hand, Spout uses streams to read or write data. This means that there is only one row kept in memory at all times, all read/written rows being freed from memory. This allows fast read/write of dataset of any size! Give it a try :)

like image 96
Adrien Avatar answered Oct 16 '22 22:10

Adrien


Spout just saved my time! I couldn't read a large file with PhpOffice/PhPSpreedSheet with many Fatal Error Memory size, and with Spout it works like a charm.

like image 28
Guillaume Avatar answered Oct 16 '22 23:10

Guillaume