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

blade84's avatar

Working with large xml/xlsx files ?

Hi, I'm working with a large xlsx file (more than 300k rows and 10-20 columns) and I need to check each row and than do create/update. I'm using FastExcel (rap2hpoutre/fast-excel)

$collection = (new FastExcel)->import($path);

foreach ($collection->chunk(20) as $items) {
   foreach ($items as $item) {
          $newData = [
	       'code' => $item['code'],
		'name' => $item['name_org'],
		'owner' => $item['fullname']
		...
	 ]; 
		
         $oldData = Product::where('code', $newData['code']->first();
	 if(!is_null($oldData) {
		// Here I check diff between old and new data
		$oldData->update($newData);
	  } else {
		Product::create($oldData);
	 }
     }
}

This code works for me, but I'm not sure is there any field where I can improve performances? First I download a file to my storage and then get a path. There are indexes in the database table. And I tried to change chunk limit (10-500+)

0 likes
5 replies
Braunson's avatar

Suggestions (some you already have done)

  • Upload file to server
  • Queue process
  • Chunk things
  • You could use updateOrCreate but I see you have a comment about checking diff between old/new data
  • Index the data
Adailton de Jesus Cerqueira Junior's avatar

I have a question about work with a large xlsx file. Are there limit how many rows Laravel can read?

I have file with more than 800k rows and even using Chunk, I get an out of memory or timeout error.

Tray2's avatar

@Adailton de Jesus Cerqueira Junior Please open your own thread with this question. No there is no limit on how many rows Laravel can handle, there are however some things you need to consider.

  1. A http request times out after 60 seconds or less, so don't process the file in the request, queue a job that handles it in the back ground.
  2. Make sure that you chunk the records, and not try to process them all at once, because it will eat up your memory pretty fast.
1 like

Please or to participate in this conversation.