How to import and export Excel and CSV files in Laravel.

Hello Geek, Importing and exporting Excel and CSV files in Laravel is a common requirement in many web applications. In this tutorial, we will learn how to import and export Excel and CSV files in Laravel.

Prerequisites

Before we begin, make sure that you have the following:

  • PHP >= 7.3.0
  • Laravel 8.x
  • Maatwebsite Excel Package: 3.1.x

Installing Maatwebsite Excel Package

To install the Maatwebsite Excel Package, run the following command:

composer require maatwebsite/excel

Exporting Excel and CSV files

To export Excel and CSV files, follow the steps below:

  1. Create a new Laravel controller using the command:
php artisan make:controller ExportController

2. In the ‘ExportController‘, add the following code:

use App\Exports\UsersExport;
use Maatwebsite\Excel\Facades\Excel;

public function export() 
{
    return Excel::download(new UsersExport, 'users.xlsx');
}

Here, we are creating a new method called ‘export()‘ that returns an Excel file of all the users in the application.

3. Create a new export class using the command:

php artisan make:export UsersExport --model=User

4. Open the ‘UsersExport ‘class in the ‘app/Exports‘ directory and add the following code:

use Maatwebsite\Excel\Concerns\FromCollection;
use App\Models\User;

class UsersExport implements FromCollection
{
    public function collection()
    {
        return User::all();
    }
}

Here, we are creating a new class called ‘UsersExport ‘that implements the ‘FromCollection ‘interface. This class retrieves all users from the ‘User ‘model and returns a collection of users.

5. Add a new route to ‘web.php:

Route::get('/export', 'App\Http\Controllers\ExportController@export');

6. Now, you can navigate to ‘/export‘ on your web application, and the Excel file will be downloaded.

To export a CSV file, you can replace ‘Excel::download‘ with ‘Excel::csv‘ in the ‘ExportController ‘method.

Importing Excel and CSV files

To import Excel and CSV files, follow the steps below:

  1. Create a new Laravel controller using the command:
php artisan make:controller ImportController

2. In the ‘ImportController‘, add the following code:

use App\Imports\UsersImport;
use Maatwebsite\Excel\Facades\Excel;

public function import(Request $request) 
{
    $file = $request->file('file');

    Excel::import(new UsersImport, $file);

    return redirect('/')->with('success', 'Users imported successfully.');
}

Here, we are creating a new method called ‘import()‘ that accepts a file as input and uses the ‘UsersImport ‘class to import the data.

3. Create a new import class using the command:

php artisan make:import UsersImport --model=User

4. Open the ‘UsersImport ‘class in the ‘app/Imports‘ directory and add the following code:

use Maatwebsite\Excel\Concerns\ToModel;
use App\Models\User;

class UsersImport implements ToModel
{
    public function model(array $row)
    {
        return new User([
            'name' => $row[0

All the best nerd!

Leave a Reply