Laravel: Import & Read Excel file (Use chunk to avoid memory fatal error)

Posted: July 5, 2016 in Laravel
Tags:

Hi, i’m using Maatwebsite/Larabel-Excel module to upload & read the Excel file in Laravel. This tools also support large Excel file as it is using ‘chunk’ to avoid PHP memory fatal error.

For the installation, please refer to the page.

Here is my Upload Form (resources/view/import.blade.php):

@extends('layouts.master')

@section('content')
            {!! Form::open( ['route' => 'import.excel', 'id' => 'form_import_excel', 'files' => true] ) !!}

Select file to upload:


            {!! Form::file('importExcel'); !!}

            
               {!! Form::submit( 'Upload', ['class' => 'btn btn-primary', 'id' => 'btn-submit-edit'] ) !!}


            {!! Form::close() !!}
         </div><!-- /.box-body -->
      </div><!-- /.col -->
</div><!-- /.row -->
@endsection

 

Route (app/Http/routes.php):

//Import Excel
 Route::get( 'import/excel', ['as' => 'import.excel', 'uses' => 'ImportController@excel']);
 Route::post( 'import/excel', ['as' => 'import.post', 'uses' => 'ImportController@postExcel']);

 

Controller (app/Http/Controllers/ImportController.php):

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use App\Http\Controllers;
use App\User;
use Validator;
use App\Repositories\AuditRepository as Audit;

use Auth;
use Flash;
use Excel;

class ImportController extends Controller
{

 /**
 * @return \Illuminate\Http\RedirectResponse
 */
 public function index()
 {
   //...
 }

 /**
 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
 */
 public function excel()
 {
     $page_title = "Import Excel";
     $page_description = "Import & Read the Excel file";

     return view('import', compact('page_title', 'page_description'));
 }
 
 public function postExcel(Request $request)
 {
     ini_set('memory_limit',-1);
 
     $destination_path = storage_path('uploads');
     $_arr_excel_ext = array('xls', 'xlsx');
 
     if (!$request->file('importExcel')->isValid()) {
        Flash::error("Sorry, there is no file attached on the form!");
        return redirect()->route('import.excel');
     }
 
     $file = $request->file('importExcel');
 
     if (!in_array($file->getClientOriginalExtension(), $_arr_excel_ext)) {
        Flash::error("Sorry, please use .xls or .xlsx extension!");
        return redirect()->route('import.excel');
     }
 
     if (!$file->move($destination_path, $file->getClientOriginalName())) {
        Flash::error("Sorry, error on loading excel!");
        return redirect()->route('import.excel');
     }

     //use 'chunk' to read large excel data file
     Excel::filter('chunk')->load($file)->chunk(250, function($results)
     {
          foreach($results as $row)
          {
                //save in database or do whatever you like here
                //print_r($row)
                echo "<br>" . $row['column_name_in_lowercase'];
 
          }
     });
 }
 
}

Good Luck.. ok Bye😉

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s