这篇文章主要介绍了关于Laravel5中使用LaravelExcel实现文件导出功能,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
一、安装
此处我安装的是maatwebsite/excel的2.1.0版本
composer require "maatwebsite/excel:~2.1.0"
安装后的设置
在config/app.php中注册服务提供者到providers数组:
Maatwebsite\Excel\ExcelServiceProvider::class,
同样在config/app.php中注册门面到aliases数组:
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
如果想要对Laravel Excel进行更多的自定义配置,执行如下Artisan命令:
php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider"
二、将数据导出
添加路由
// excel exportRoute::get('/monitor/export','Admin\ExcelController@export')->name('monitor.export');创建控制器
php artisan make:controller Admin/ExcelController
在控制中添加一下代码
<?php
namespace 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::download(new CunliangExport, 'invoices.xlsx');
$data = Monitor::get()->toArray();
return Excel::create('数据更新', function($excel) use ($data) {
$excel->sheet('数据更新', function($sheet) use ($data)
{
$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');
}
}在blade模板文件添加以下代码
.
.
.<p class="box-header">
<a class="btn btn-success" href="{{route('monitor.export')}}">导出</a></p>.
.
.根据自己需求更改即可。
相关推荐:
以上就是Laravel5中使用LaravelExcel实现文件导出功能的详细内容,更多请关注php中文网其它相关文章!
全网最新最细最实用WPS零基础入门到精通全套教程!带你真正掌握WPS办公! 内含Excel基础操作、函数设计、数据透视表等
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号