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

laravel 5 excel and pdf tutorial : How to 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

Maatwebsite\Excel\ExcelServiceProvider::class,

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)

<?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();
      view()->share('blogs',$blogs);
      // if request has pdf
      if($req->has('downloadpdf')){
        $pdf = PDF::loadView('pdf')->setPaper('a4', 'landscape');
        return $pdf->download('pdf');
      }
      // if request has excel
      if($req->has('downloadexcel')){
        Excel::create('users', function($excel) use ($blogs) {
          $excel->sheet('Sheet 1', function($sheet) use ($blogs) {
            $sheet->fromArray($blogs);
          });
        })->export('xls');
      }
      // return index page
      return view('index');
    }
}

Models (datatoPDF.php)

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

Routes (Web.php)

Route::get('/',array('as'=>'htmltopdf','uses'=>'PDFController@index'));

Views (index.blade.php)


<!DOCTYPE html>
<html lang="en">
  <head>
    <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="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">

    <!-- 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="https://oss.maxcdn.com/libs/html5shiv/3.7.2/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>

    <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>
        </button>
        <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>
        </ul>
      </div>
      <table class="table table-striped">
        <thead>
          <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Email</th>
            <th>Password</th>
            <th>Created At</th>
            <th><button class="btn btn-success btn-xs">Add New Supplier</button></th>
          </tr>
        </thead>
        <tbody>
          @foreach($blogs as $blog)
            <tr>
              <td>{{ $blog->id }}</td>
              <td>{{ $blog->name }}</td>
              <td>{{ $blog->email }}</td>
              <td>{{ $blog->password }}</td>
              <td>{{ $blog->created_at }}</td>
              <td>
                <button class="btn btn-warning btn-xs btn-detail">
                  <span class="glyphicon glyphicon-edit"></span>
                </button>
                <button class="btn btn-danger btn-xs btn-delete">
                  <span class="glyphicon glyphicon-trash"></span>
                </button>
              </td>
            </tr>
          @endforeach
        </tbody>
      </table>
    </div>
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
  </body>
</html>

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

COMMENTS


Feel free to code it up and send us a pull request.

Hi everyone, let's me know how much this lesson can help your work. Please Subscribe and Follow Our Social Media 'kodeajaib[dot]com' to get Latest tutorials and will be send to your email everyday for free!, Just hit a comment if you have confused. Nice to meet you and Happy coding :) all ^^



Follow by E-Mail


Name

ADO.NET,3,Ajax,6,Android,9,AngularJS,4,ASP.NET,4,Blogger Tutorials,7,Bootstrap,7,C++,1,Codeigniter,2,Cplusplus,6,Crystal Report,6,CSharp,25,Ebook Java,2,FlyExam,1,FSharp,3,Game Development,2,Java,35,JDBC,2,Laravel,89,Lumen,2,MariaDB,2,Ms Access,3,MySQL,31,ODBC,6,OleDB,1,PHP,14,PHP Framework,6,PHP MYSQLI,9,PHP OOP,5,Python,8,Python 3,4,SQL Server,4,SQLite,4,Uncategorized,5,Vb 6,2,Vb.Net,89,Video,48,Vue Js,4,WPF,2,Yii,3,
ltr
item
KODE AJAIB: Laravel 5 Tutorial : Laravel Export Data to an Excel Spreadsheet or PDF Files
Laravel 5 Tutorial : Laravel Export Data to an Excel Spreadsheet or PDF Files
laravel 5 excel and pdf tutorial : How to export data to an Excel Spreadsheet or PDF Files.
https://i.ytimg.com/vi/F6GSD3GXo3Y/hqdefault.jpg
https://i.ytimg.com/vi/F6GSD3GXo3Y/default.jpg
KODE AJAIB
https://scqq.blogspot.com/2017/01/laravel-5-tutorial-laravel-export-data.html
https://scqq.blogspot.com/
https://scqq.blogspot.com/
https://scqq.blogspot.com/2017/01/laravel-5-tutorial-laravel-export-data.html
true
3214704946184383982
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy