Using Laravelexcel to implement file export function in Laravel5

Source: Internet
Author: User

This article mainly introduces the use of Laravelexcel in the Laravel5 to achieve file export function, has a certain reference value, now share to everyone, the need for friends can refer to

Laravel5 using Laravel/excel to implement Excel/csv file export function

First, installation
Here I install the Maatwebsite/excel 2.1.0 version

Composer require "maatwebsite/excel:~2.1.0"

Post-installation settings
Register the service provider to the providers array in config/app.php:


Also register the façade to the aliases array in config/app.php:

' Excel ' = Maatwebsite\excel\facades\excel::class,

If you want to make more custom configurations for Laravel Excel, perform the following artisan command:

PHP artisan vendor:publish--provider= "Maatwebsite\excel\excelserviceprovider"

Second, export the data
Add route

Excel exportroute::get ('/monitor/export ', ' Admin\excelcontroller@export ')->name (' Monitor.export ');

Create a Controller

PHP Artisan Make:controller Admin/excelcontroller

Add code to the control

<?phpnamespace App\http\controllers\admin;use Illuminate\http\request;use App\http\controllers\controller;use Excel;use app\models\monitor;use app\exports\cunliangexport;class Excelcontroller extends Controller{public function        Export () {//return Excel::d ownload (New Cunliangexport, ' invoices.xlsx ');                $data = Monitor::get ()->toarray (); return excel::create (' Data Update ', function ($excel) use ($data) {$excel->sheet (' Data Update ', function ($sheet) use ($dat   A) {$sheet->cell (' A1 ', function ($cell) {$cell->setvalue (' update_date ');                                });   $sheet->cell (' B1 ', function ($cell) {$cell->setvalue (' File_type ');                                });   $sheet->cell (' C1 ', function ($cell) {$cell->setvalue (' file_num ');                                });   $sheet->cell (' D1 ', function ($cell) {$cell->setvalue (' space_size ');                                }); $sheet->cell (' E1 ', function ($cell) {$cell->setvalue (' exec_time ');                                });   $sheet->cell (' F1 ', function ($cell) {$cell->setvalue (' created_at ');                                });                                        if (!empty ($data)) {foreach ($data as $key = = $value) {                                        $i = $key +2;                                        $sheet->cell (' A '. $i, $value [' update_date ']);                                        $sheet->cell (' B '. $i, $value [' File_type ']);                                        $sheet->cell (' C '. $i, $value [' file_num ']);                                        $sheet->cell (' D '. $i, $value [' space_size ']);                                        $sheet->cell (' E '. $i, $value [' exec_time ']);                    $sheet->cell (' F '. $i, $value [' created_at ']);        }                }            });    })->download (' xlsx '); }}

Add the following code to the blade template file

... <p class= "Box-header" >    <a class= "btn btn-success" href= "{{route (' Monitor.export ')}}" > Export </a ></p>, .....

Change it according to your needs.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.