How To Export Excel File In Laravel 9

In this article, we will see how to export an excel file in laravel 9. Here, we will learn to export data in excel in laravel 8 and laravel 9. In laravel  9, we will use maatwebsite/excel package for exporting data. it provides an easy way for exporting the data as a CSV file or Excel file.

So, let's see the laravel 9 export excel file, laravel excel export example, export in laravel 9, export excel in laravel 8/9, and maatwebsite/excel laravel 9.

Laravel Excel is intended at being Laravel-flavoured PhpSpreadsheet: a simple, but an elegant wrapper around PhpSpreadsheet with the goal of simplifying exports and imports.

 

Step 1: Install Laravel 9 for Export Excel File

In this step, we will install laravel 9 using the following command,

composer create-project --prefer-dist laravel/laravel laravel_export_excel

 

 

Step 2: Configure Database

In this step, we will configure the database details. So, open the .env file and add the details. 

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_9_export_excel
DB_USERNAME=root
DB_PASSWORD=root

 

Step 3: Install maatwebsite/exel Package

Now, we will install the maatwebsite/excel package using the composer command.

composer require maatwebsite/excel

If composer require fails on laravel 9 because of the simple-cache dependency, you will have to specify the psr/simple-cache version as ^2.0 in your composer.json to satisfy the PhpSpreadsheet dependency. You can install both at the same time:

composer require psr/simple-cache:^2.0 maatwebsite/excel

The Maatwebsite\Excel\ExcelServiceProvider is auto-discovered and registered by default.

If you want to register it yourself, add the ServiceProvider in config/app.php:

'providers' => [
    /*
     * Package Service Providers...
     */
    Maatwebsite\Excel\ExcelServiceProvider::class,
]

The Excel facade is also auto-discovered.

If you want to add it manually, add the Facade in config/app.php:

'aliases' => [
    ...
    'Excel' => Maatwebsite\Excel\Facades\Excel::class,
]

To publish the config, run the vendor publish command:

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

This will create a new config file named config/excel.php.

 

Step 4: Create Dummy Data Using Tinker

After adding aliases and providers are adding some dummy records in the database using the below command.

php artisan tinker

factory(App\User::class, 50)->create();

 

 

Step 5: Add Route

In this step, we are creating a new route in the web.php file. So, add the following code to that file.

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\ExportExcelController;
  
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
 
Route::controller(ExportExcelController::class)->group(function(){
    Route::get('index', 'index');    
    Route::get('export/excel', 'exportExcelFile')->name('export.excel');
});

 

Step 6: Create ExportExcelController

Now, we will create the ExportExcelController using the following command.

php artisan make:controller ExportExcelController

After running this command we will add the following code to the controller.

App/Http/Controllers/ExportExcelController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Exports\ExportUsers;
use Maatwebsite\Excel\Facades\Excel;

class ExportExcelController extends Controller
{
     public function index()
    {
       return view('index');
    }

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

 

Step 7: Create Export Class

Now, we will create the export class using the below command.

php artisan make:export ExportUsers --model=User

After running this command you will find the ExportUsers.php file.

app/Export/ExportUsers.php

<?php

namespace App\Exports;

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

class ExportUsers implements FromCollection
{
    /**
    * @return \Illuminate\Support\Collection
    */

    public function collection()
    {
        return User::all();
    }
}

 

 

Step 8: Create Blade File for View

Now, we will create the index.blade.php file. So, add the following code to that file.

resources/views/index.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
  <title>How To Export Excel File In Laravel 9 - Techsolutionstuff</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
	<h3>How To Export Excel File In Laravel 9 - Techsolutionstuff</h3>
	<form action="#" method="POST" name="importform"
	  enctype="multipart/form-data">
		@csrf		
		<div class="form-group">
			<a class="btn btn-info" href="{{ route('export.excel') }}">Export Excel File</a>
		</div> 
	</form>
</div>
</body>
</html>

 

Step 9: Run Export Excel File

Now, we will run laravel export excel file using maatwebsite/excel using the following command.

php artisan serve

 


You might also like:

RECOMMENDED POSTS

FEATURE POSTS