Laravel export table data to excel sheet using Laravel excel

Laravel export table data to excel sheet using Laravel Excel. Many times we need to export user data and product and order data. So let’s see, The easiest way to get the data table data using eloquent model and export data to excel file.

Today we are learning the export user data to excel file. For exporting user data to excel file, first of all, we are getting the user data using eloquent model and export the excel file.

For generating the excel file we need to install the Laravel Excel package. So let’s install Laravel package first.

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 start the learning how to export user data to excel file format.

Let’s create the `exportUser` method to your UserController like

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;

class UserController extends Controller
{

    public function getBladeExlce()
    {
        $userData = User::all();

        \Excel::create('thecodingstuff', function($excel) use($userData) {

            $excel->sheet('user', function($sheet) use($userData) {

                $excelData = [];
                $excelData[] = [
                    'Id',
                    'First Name',
                    'Last Name',
                    'Name',
                    'E-mail',
                    'Password',
                    'Mobile',
                    'Country',
                    'State',
                    'City',
                    'Pincode',
                    'Created',
                    'Updated',
                ];

                foreach ($userData as $key => $value) {
                    $excelData[] = [
                        $value->id,
                        $value->first_name,
                        $value->last_name,
                        $value->name,
                        $value->email,
                        $value->password,
                        $value->mobile,
                        $value->country,
                        $value->state,
                        $value->city,
                        $value->pincode,
                        $value->created_at,
                        $value->updated_at
                    ];                    
                }

                $sheet->fromArray($excelData, null, 'A1', true, false);

            });

        })->download('xlsx');

    }
}

So Let’s see it’s too easy to export user data to excel file.

If you have any question or face any issue 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