Easy Excel exports in Laravel

March 2, 2018 By - Usama Muneer | 110

    Exporting Excel files come handy when you’re building some inventory kind of an application. In this short tutorial, we’ll go through on exporting excel files with Laravel & look on for some crazy hacks and tricks in between.

    Installing the package

    composer require maatwebsite/excel

    Add the Service Provider & Facade

    Right into your config/app.php, add the service provider.

    'providers' => [

    and the facade

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

    Setup a simple route

    Route::get( '/export', 'DataExporter@index' );

    Add the functionality to the DataExporter Class

    class DataExporter {
       public function index() {
          \Excel::create( 'file_name', function ( $excel ) {
             /** Set the spreadsheet title, creator, and description */
             $excel->setTitle( 'your_file_title' );
             $excel->setCreator( 'creator_name' )->setCompany( 'company_name' );
             $excel->setDescription( 'This is the description of the exported excel file' );
             /** Creating a simple sheet */
             $excel->sheet( 'sheet_name', function ( $sheet ) {
                /** Manipulating Rows */
                $sheet->row( '1', array(
                   'First Row Heading'
                ) );
                $users = User::all();
                 * Filter through each of users
                 * @var  $key
                 * @var  $value
                foreach ( $users as $key => $value ) {
                   $sheet->SetCellValue( "A" . $key, $value->name );
             } );
          } )->get( 'xlsx' );


    Well thats it, simply hit the /export in your browser and you’ll get a popup to download your excel file. Generating an excel export was never this simple before. For more information about this awesome package, check out the documentation website.


    blog user
    Usama Muneer

    A web enthusiastic, self-motivated & detail-oriented professional Full-Stack Web Developer from Karachi, Pakistan with experience in developing applications using JavaScript, WordPress & Laravel specifically. Loves to write on different web technologies with an equally useful skill to make some sense out of it.

    Related Posts

    While Developing applications in Laravel, there comes situations where you have date in simple string format. For instance getting raw response from an API. But you want to convert that string into Carbon instance and use the Carbon helpers on it. Well there's a simple solution to it.

    Laravel ships in with the awesomeness of Database Migrations which allows you to version control your database. But there are number of events when you are required to change the datatypes or attributes assigned to your table columns inside of your migrations.

    Sometimes you need to provide dropdowns in your exported excel files for more enhanced look on the data. Here's how we can achieve it.

    If you want your table to have literally one of anything, then one to one is the most simplest relationship. For instance, a token is generated for resetting a user password & its a simple one to one relationship between users and password_resets table.