How To Set Header In maatwebsite/excel Export In Laravel

How To Set Header In maatwebsite/excel Export In Laravel

In this post we will give you information about How To Set Header In maatwebsite/excel Export In Laravel. Hear we will give you detail about How To Set Header In maatwebsite/excel Export In LaravelAnd how to use it also give you demo for it if it is necessary.

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', '[email protected]')->name('user-list');

// Export User Table Data In Excel With Set Header
Route::get('user-export', '[email protected]')->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 AppHttpControllers;

use IlluminateHttpRequest;
use IlluminateSupportFacadesInput;
use Redirect;
use Excel;
use AppUser;

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('onlinecode', 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']); 

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.


<div >
    <div >
        <div >
            <a href="{{ URL::to('user-export/xlsx') }}" >Export Excel</a>
            <a href="{{ URL::to('user-export/csv') }}" >Export CSV</a>
            <div >
                <div >User List</div>

                <div >
                    <table >
                                <th>First Name</th>
                                <th>Last Name</th>
                            @if(!empty($data) && $data->count())
                                @foreach($data as $key => $value)
                                        <td>{{ $value->firstname }}</td>
                                        <td>{{ $value->lastname }}</td>
                                        <td>{{ $value->email }}</td>

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:


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

Hope this code and post will helped you for implement How To Set Header In maatwebsite/excel Export In Laravel. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US