How to Export Data in an Excel File in Laravel 9?

Patrick Brouwers created a well-reputed package that imports and exports data in the Laravel framework. It has 49.4 million downloads at the time of writing this article which proves it is one of the best packages and the Laravel community loved it.

Requirements:

Before installing maatwebsite/excel package, make sure your web server meets the following requirements:

  • PHP: ^7.2 OR ^8.0
  • Laravel Version: ^5.8
  • PhpSpreadsheet: ^1.21
  • psr/simple-cache: ^1.0
  • PHP extension php_zip enabled
  • PHP extension php_xml enabled
  • PHP extension php_gd2 enabled
  • PHP extension php_iconv enabled
  • PHP extension php_simplexml enabled
  • PHP extension php_xmlreader enabled
  • PHP extension php_zlib enabled

Installations:

To install the maatwebsite/excel package, run the below composer command in the terminal.

composer require maatwebsite/excel

It takes a few seconds to install the package in the Laravel application. In the next step register the excel service provide, so open config/app.php and add the below class into the providers array,

Maatwebsite\Excel\ExcelServiceProvider::class,

After this, the facade also needs to be added to the same file. So add the below Facade in the aliases array,

'Excel' => Maatwebsite\Excel\Facades\Excel::class,

The next and last part of the installation is to publish the vendors. Run the below command to do it,

php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider" --tag=config

Now we have completed the installation steps, next, let’s export data into an excel file.

Export Data to Excel File

If you install this Laravel package on a fresh application then you need some data in the users table to export. You can generate dummy data through the Laravel factory.

To export data, maatwebsite/excel package wants us to run below command that creates class and directory in the app folder

php artisan make:export UsersExport --model=User

The above command creates UsersExport class with a collection method that returns all users through the eloquent all() method.

namespace App\Exports;

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

class UsersExport implements FromCollection
{
    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        return User::all();
    }
}

Next, create a UserController and import excel facade and UsersExport class. Then create exportUsersWithExcel() method in the UserController and use download() method from excel facade.

namespace App\Http\Controllers;

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

class UserController extends Controller
{
    public function exportUsersWithExcel() 
    {
        return Excel::download(new UsersExport, 'users-data.xlsx');
    }
}

Next, create a route in the routes/web.php to run exportUsersWithExcel() method that exports and downloads users data with an excel file.

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;


Route::get('/export-users-with-excel', [UserController::class, 'exportUsersWithExcel']);

Now let’s test it, run the below command that serves the application on development server and provides URL to access

php artisan serve

Then open browser and open below URL

http://127.0.0.1:8000/export-users-with-excel

When you open it, it will export users’ data into an excel file and download it.

Conclusion

In this article, we learned how to install an excel package in a Laravel application and export data in an excel file. If you would like to learn more about it, check out our Laravel page.