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:
Maatwebsite\excel\excelserviceprovider::class,
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.