Easy Excel exports in Laravel

Last Updated at : March 2, 2018 By - Usama Muneer | 1305

    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' => [
    
       Maatwebsite\Excel\ExcelServiceProvider::class,
    
    ]

    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' );
     }
    }

    Conlcusion

    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

    Previously Localization in Laravel was done by creating separate directories for each locales and place in under resources/lang/ and add key value pairs to every single keyword in a number of files as per their modules.

    Another #LaraconUS in the books & It was the 6th instalment of #LaraconUS happened at Chicago with full swing & we got a lot to learn this time with exceptional line of speakers and never-ending community support by fellow Artisans

    Laravel is one of a famous and widely used PHP framework which is growing with immense pace since its version 4.2. Learning Laravel & Setting up your application locally is not much of a difficult task but when it comes to deployment, developers often find it a bit tricky to handle.

    There are several PHP frameworks for a web development project. Among them Laravel is regarded as the best open-source PHP web framework.