• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Laravel5中使用LaravelExcel实现文件导出功能

php 搞代码 4年前 (2022-01-22) 13次浏览 已收录 0个评论

这篇文章主要介绍了关于Laravel5中使用LaravelExcel实现文件导出功能,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

Laravel5中使用Laravel/Excel实现Excel/CSV文件导出功能

一、安装
此处我安装的是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

在控制中添加一下代码

<?phpnamespace App\Http\Controll<p style="color:transparent">2本文来源gao!daima.com搞$代!码网</p><span>搞代gaodaima码</span>ers\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实现模糊匹配功能

Laravel5.5新特性报错

以上就是Laravel5中使用LaravelExcel实现文件导出功能的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Laravel5中使用LaravelExcel实现文件导出功能

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址