Laravel export table data to excel sheet using Maat website package

Laravel export table data to excel sheet using Maat website package

  • 14 July, 2018
  • Renish Khunt
  • Laravel

Many times we need to export Users, Products, Orders etc… to the Excel file. Using Laravel Maat Website package we can easily fetch data from database using eloquent model then export database record to excel file using Laravel Maat website package. Let’s see Laravel export table data to excel sheet using Maat website package.

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.

First, we need to install Laravel Maat website package for export data to excel file.

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.

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.

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

Laravel export table data to excel sheet

<?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.