Laravel

Laravel: Installing maatwebsite/excel and Exporting Excel Files

This time, I will show you how to install maatwebsite/excel in Laravel and export Excel files.

Installation Environment for maatwebsite/excel

The Laravel version is 6.18.39, installed on Sakura Rental Server (Standard Plan).

Installing maatwebsite/excel

Run the maatwebsite/excel installation command (Sakura Rental Server)

Move (cd) to the directory where Laravel is running and execute the following command. You need to specify the location of composer.phar.

php /home/{server_contract_username}/www/{laravel_running_directory}/composer.phar require maatwebsite/excel

Run the maatwebsite/excel installation command (Environment where the composer command can be executed)

Other reference sites mentioned that the following command would work, but in my Sakura Rental Server environment, it returned “composer command not found,” so I executed the above command.

composer require maatwebsite/excel

 

If an error occurs when running the maatwebsite/excel installation command

When I ran the maatwebsite/excel installation command on Laravel 6.18.39,

~
Problem1
~
Problem2
~
Problem3
~

these errors were displayed.
Although I didn’t save the exact output, I believe it was something like “installation not possible with the framework version specified in composer.json” when trying to install maatwebsite/excel.
So, before installing maatwebsite/excel, I executed the following Laravel update command.

php /home/{server_contract_username}/www/{laravel_running_directory}/composer.phar update

 
After that, running the “Run the maatwebsite/excel installation command (Sakura Rental Server)” allowed me to install maatwebsite/excel.

Register the Service Provider and Facade in the Config File (/config/app.php)

In the providers section of the /config/app.php file, add the following:

'providers' => [
    ~
    Maatwebsite\Excel\ExcelServiceProvider::class,
],

In the aliases section of the /config/app.php file, add the following:

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

],

Move (cd) to the directory where Laravel is running and execute the following artisan command:

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

Running the artisan command will generate the execl.php file in:

/config/execl.php

Prepare the users Table for Excel Export

The following users table was prepared:

users table

Name Description
id Primary Key
name User Name
email Email Address

Create a Class to Output All Contents of the users Table to the View

Create the following class (/app/Exports/UsersExport.php file) to output all contents of the users table to the view (exports/users.blade.php):

<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromView;
use Illuminate\Contracts\View\View;
use App\User;

class UsersExport implements FromView
{

    public function view(): View
    {
        return view('exports.users', [
            'users' => User::all()
        ]);
    }
}
?>

View for Excel File Output (/exports/users.blade.php)

The view (/resources/views/exports/users.blade.php) for the content to output to the Excel file is as follows. It outputs “name” and “email” to the Excel file.

<p>user table</p>
<table>
  <thead>
  <tr>
      <th>Name</th>
      <th>Email</th>
  </tr>
  </thead>
  <tbody>
  @foreach($users as $user)
      <tr>
          <td>{{ $user->name }}</td>
          <td>{{ $user->email }}</td>
      </tr>
  @endforeach
  </tbody>
</table>

Controller to Output the Excel File (users.xlsx)

The Controller (/app/Http/Controllers/UsersController.php file) to output the users table contents to an Excel file (users.xlsx) is as follows:

<?php
namespace App\Http\Controllers;

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

class UsersController extends Controller
{
    
    public function export(){

	    return Excel::download(new UsersExport, 'users.xlsx');

    }
}
?>

Excel Output URL (/routes/web.php)

Add the Excel (users.xlsx) output URL to /routes/web.php:

<?php

Route::get('/usersexport', 'UsersController@export');

?>

Excel Output Result

Accessing the following URL will download the Excel file “users.xlsx”:

https:// ~ /usersexport

The output image of “users.xlsx” is as follows:

 


 
*If you reuse this, please do so at your own risk.