Laravel create excel file directly from blade view using Laravel Excel Maatwebsite

Laravel create the excel file directly from the blade view HTML table. It’s too easy and the fastest way to generate excel file from HTML table. Without blade view file we need to create the code for the generating the excel file. We need to set the array to the proper column format for generating the excel sheet. Now, forget all those things and just create the table on the Laravel blade view file.

Let’s see the demo how we can generate the excel file from the HTML table blade view example.

I hope you installed the Laravel if not follow this tutorial.

First of all, we need to install the Excel library on Laravel.

Let’s open the composer.json file located at the root of the Laravel. Then create the entry of the Excel library like below.

For Laravel 5

"maatwebsite/excel": "~2.1.0"

For Laravel 4

"maatwebsite/excel": "~1.3"

After, adding the excel library let’s update the composer for downloading the new library.

composer update

Fire “composer update” command from the root of the Laravel installed. Wait sometime still updating the composer.

After composer update let’s register the Excel library on the Laravel. We need to add the Service Provide and Facades entry to our Laravel setup like.

For registering the service provider let’s open the “config/app.php” file of the Laravel and add service provider.

Maatwebsite\Excel\ExcelServiceProvider::class,

Let’s register facade on the same file.

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

Awesome, We register the Service Provider and Facades on the Laravel. Now, Let’s generate the config file for the Excel Library.

For Laravel 5

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

The config files can now be found at config/excel.php

For Laravel 4

php artisan config:publish maatwebsite/excel

The config files can now be found at app/config/packages/maatwebsite/excel

The Excel Library installation process is finished. Now, let’s create the blade file with the HTML table.

First of all, We create the route for our page. Let’s open the route file routes/web.php then enter the route like

Route::get('thecodingstuff/excel', 'ExcelController@getBladeExlce');

The Route is created for our page now let’s create the controller.

Let’s create the controller file at app/Http/Controllers/ExcelController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ExcelController extends Controller
{

    public function __construct(){

    }

    public function getBladeExlce(){

        \Excel::create('thecodingstuff', function($excel) {
            $excel->sheet('thecodingstuff', function($sheet) {
                $sheet->loadView('bladeexcel.bladexcel');
            });
        })->download('xls');
        return view('thecodingstuff.bladexcel');

    }

}

Now, let’s create the bale view file at “resources/views/thecodingstuff/bladexcel.blade.php” then add the HTML like.

<html>
    <body>
        <table border="1">
            <tr>
                <th>ID</th>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Username</th>
                <th>E-mail</th>
                <th>Password</th>
                <th>Mobile</th>
                <th>Address 1</th>
                <th>Address 2</th>
                <th>City</th>
                <th>State</th>
                <th>Country</th>
            </tr>
            <tr>
                <td>1</td>
                <td>TheCoding</td>
                <td>Stuff</td>
                <td>thecodingstuff</td>
                <td>example@gmail.com</td>
                <td>ds#@dfi$udf</td>
                <td>9999888875</td>
                <td>71 Pilgrim Avenue</td>
                <td>Chevy Chase, MD 20815</td>
                <td>Chevy Chase</td>
                <td>MD</td>
                <td>United State</td>
            </tr>
            <tr>
                <td>2</td>
                <td>TheCoding</td>
                <td>Stuff</td>
                <td>thecodingstuff</td>
                <td>example@gmail.com</td>
                <td>ds#@dfi$udf</td>
                <td>9999888875</td>
                <td>71 Pilgrim Avenue</td>
                <td>Chevy Chase, MD 20815</td>
                <td>Chevy Chase</td>
                <td>MD</td>
                <td>United State</td>
            </tr>
            <tr>
                <td>3</td>
                <td>TheCoding</td>
                <td>Stuff</td>
                <td>thecodingstuff</td>
                <td>example@gmail.com</td>
                <td>ds#@dfi$udf</td>
                <td>9999888875</td>
                <td>71 Pilgrim Avenue</td>
                <td>Chevy Chase, MD 20815</td>
                <td>Chevy Chase</td>
                <td>MD</td>
                <td>United State</td>
            </tr>
            <tr>
                <td>4</td>
                <td>TheCoding</td>
                <td>Stuff</td>
                <td>thecodingstuff</td>
                <td>example@gmail.com</td>
                <td>ds#@dfi$udf</td>
                <td>9999888875</td>
                <td>71 Pilgrim Avenue</td>
                <td>Chevy Chase, MD 20815</td>
                <td>Chevy Chase</td>
                <td>MD</td>
                <td>United State</td>
            </tr>
            <tr>
                <td>5</td>
                <td>TheCoding</td>
                <td>Stuff</td>
                <td>thecodingstuff</td>
                <td>example@gmail.com</td>
                <td>ds#@dfi$udf</td>
                <td>9999888875</td>
                <td>71 Pilgrim Avenue</td>
                <td>Chevy Chase, MD 20815</td>
                <td>Chevy Chase</td>
                <td>MD</td>
                <td>United State</td>
            </tr>
        </table>
    </body>
</html>

The generate the excel file from the Laravel blade view code is ready. Now, just change the excel file name as you want. then run the URL to download the excel file.

http://localhost:8000/thecodingstuff/excel

Downloading the excel file it’s too easy than the old export method generate the excel file using the PHP array.

I hope everything is working for you if not please write the comment below.

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