Laravel 5 Tutorial : Laravel Export Data to an Excel Spreadsheet or PDF Files

Laravel 5.3 tutorial - How to export data from database into an Excel Spreadsheet or PDF Files in laravel 5.3?

Laravel Excel brings the power of PHPOffice's PHPExcel to Laravel 5 with a touch of the Laravel Magic. It includes features like: importing Excel and CSV to collections, exporting models, array's and views to Excel, importing batches of files and importing a file by a config file.

At the previews lessons, we have learn how to export data to PDF, so please read How to Export to PDF using Dompdf Library with Example.

Video Tutorial Laravel Export Data to an Excel Spreadsheet or PDF Files

Full Source Code

Installing Laravel-Ecel

Require this package in your composer.json and update composer. This will download the package and PHPExcel of PHPOffice.

"maatwebsite/excel": "~2.1.0"

After updating composer, add the ServiceProvider to the providers array in config/app.php


You can use the facade for shorter code. Add this to your aliases:

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

Publish the config settings in Laravel 5

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

Controller (PDFController.php)


namespace App\Http\Controllers;
use Illuminate\Http\Request;
use PDF; // pdf namespace
use Excel; // Excel namespace

use App\datatoPDF;
class PDFController extends Controller
    // show all data
    public function index(Request $req)
      // show all data to index
      $blogs = datatoPDF::all();
      // if request has pdf
        $pdf = PDF::loadView('pdf')->setPaper('a4', 'landscape');
        return $pdf->download('pdf');
      // if request has excel
        Excel::create('users', function($excel) use ($blogs) {
          $excel->sheet('Sheet 1', function($sheet) use ($blogs) {
      // return index page
      return view('index');

Models (datatoPDF.php)


namespace App;

use Illuminate\Database\Eloquent\Model;

class datatoPDF extends Model
    protected $table = 'users';

Routes (Web.php)


Views (index.blade.php)

<!DOCTYPE html>
<html lang="en">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>All Users Data</title>

    <!-- Bootstrap -->
    <link rel="stylesheet" href="">
    <link rel="stylesheet" href="">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src=""></script>
      <script src=""></script>

    <div class="container">
      <h2>All Users Data</h2>
      <div class="btn-group">
        <button class="btn btn-primary btn-xs dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          Export All Data <span class="caret"></span>
        <ul class="dropdown-menu">
          <li><a href="{{ route('htmltopdf',['downloadpdf'=>'pdf']) }}">Export to PDF</a></li>
          <li><a href="{{ route('htmltopdf',['downloadexcel'=>'excel']) }}">Export to Excel</a></li>
      <table class="table table-striped">
            <th>Created At</th>
            <th><button class="btn btn-success btn-xs">Add New Supplier</button></th>
          @foreach($blogs as $blog)
              <td>{{ $blog->id }}</td>
              <td>{{ $blog->name }}</td>
              <td>{{ $blog->email }}</td>
              <td>{{ $blog->password }}</td>
              <td>{{ $blog->created_at }}</td>
                <button class="btn btn-warning btn-xs btn-detail">
                  <span class="glyphicon glyphicon-edit"></span>
                <button class="btn btn-danger btn-xs btn-delete">
                  <span class="glyphicon glyphicon-trash"></span>
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src=""></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src=""></script>

Download Full source code Laravel Export Data to an Excel Spreadsheet or PDF Files

If you found Sector Code helpful, amazing and awesome, please like, share and join us in youtube. See you next lessons...

Get Latest Source Code For FREE!

Hi guys, let me introduce myself. My name is Harison, I work at an Private Company in Indonesia 'CV.Delta Microtech' as a .Net Programmer since 2010.
Please Subscribe and Follow Our Social Medias 'Sector Code' to get Latest tutorials and will be send to your email everyday for free!, Nice to meet you and Happy coding :) all ^^

Buy me a Bear, you can send me your donations to my Wallet address on below :