How To Set Header In maatwebsite/excel Export In Laravel

Today, we are share with you how to set header in excelsheet when you are export excel in laravel. mostlly all developer use maatwebsite/excel package in laravel application for excel import/export. so, we are here use this package for demo tutorial.

Set header in excel during export data from database by dynamic way is this most common task in most of laravel application. but many new laravel developer did't know proper how to set header in excel. so, simply follow this step for set header in excel.

If you don't know how to install and configure maatwebsite/excel package in laravel, please visit this tutorial link How To Excel Import/Export In Laravel

Step : 1 Create Route

First we are create one route for export excel from users table. so, open your routes/web.php file and add following routes.


// Display All User In Table
Route::get('user-list', 'HomeController@userList')->name('user-list');

// Export User Table Data In Excel With Set Header
Route::get('user-export', 'HomeController@exportUserData')->name('user-export');

Step : 2 Create Method In HomeController

Now, open app/Http/Controllers/HomeController.php file and add following two function. you also use any controller.


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Redirect;
use Excel;
use App\User;

class HomeController extends Controller
{    
    public function userList()
    {
    	$data = \DB::table('users')->get();
        return view('userList', compact('data'));
    }

    public function exportUserData($type)
    {
    	$data = User::get()->toArray();
        return Excel::create('laravelcode', function($excel) use ($data) {
            $excel->sheet('mySheet', function($sheet) use ($data)
            {
                $sheet->cell('A1', function($cell) {$cell->setValue('First Name');   });
                $sheet->cell('B1', function($cell) {$cell->setValue('Last Name');   });
                $sheet->cell('C1', function($cell) {$cell->setValue('Email');   });
                if (!empty($data)) {
                    foreach ($data as $key => $value) {
                        $i= $key+2;
                        $sheet->cell('A'.$i, $value['firstname']); 
                        $sheet->cell('B'.$i, $value['lastname']); 
                        $sheet->cell('C'.$i, $value['email']); 
                    }
                }
            });
        })->download($type);
    }
}
	
Step : 3 Create userList.blade.php File

Next, we are create userList.blade.php file in resources/views/ folder and copy following code in it.


@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <a href="{{ URL::to('user-export/xlsx') }}" class="btn btn-success">Export Excel</a>
            <a href="{{ URL::to('user-export/csv') }}" class="btn btn-danger">Export CSV</a>
            <div class="panel panel-default">
                <div class="panel-heading">User List</div>

                <div class="panel-body">
                    <table class="table table-bordered">
                        <thead>
                            <tr>
                                <th>First Name</th>
                                <th>Last Name</th>
                                <th>Email</th>
                            </tr>
                        </thead>
                        <tbody>
                            @if(!empty($data) && $data->count())
                                @foreach($data as $key => $value)
                                    <tr>
                                        <td>{{ $value->firstname }}</td>
                                        <td>{{ $value->lastname }}</td>
                                        <td>{{ $value->email }}</td>
                                    </tr>
                                @endforeach
                            @endif
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection	
	

Now we are ready to run our example so run bellow command ro quick run:


php artisan serve

Now you can test one by one route url in your browser like that:


http://localhost:8000/user-list

We are hope you like this tutorials, if any question regarding any query please post your question in our forums click on bellow link Laravelcode's Forums

 Prev post

Next post