Laravel Excel 读写插件 轻松操作Excel

阅读 (3627)
Laravel Excel旨在成为Laravel风格的PhpSpreadsheet:一个简单但优雅的PhpSpreadsheet包装器,旨在简化导出。

安装 Introduction

🔥PhpSpreadsheet是一个用纯PHP编写的库,它提供了一组类,允许您读取和写入不同的电子表格文件格式,如Excel和LibreOffice Calc。

Laravel Excel功能:

  • 轻松将集合导出到Excel
  • 使用自动分块导出查询以获得更好的性能
  • 队列导出以获得更好的性能
  • 轻松将Blade视图导出到Excel

https://medium.com/@maatwebsite/laravel-excel-lessons-learned-7fee2812551

支持的版本

版本将在有限的时间内得到支持。

插件版本 Laravel 版本 Php 版本 支持
2.1 <=5.6 <=7.0 2018-05-15停止维护
3.0 ^5.5 ^7.0 新特性

路线图 Roadmap

3.0目前不支持导入excel内容。此功能将在3.1中重新添加。如果需要用到导入,请用2.1版本

安装 Installation

composer.jsonLaravel项目中需要此包。这将下载包和PhpSpreadsheet。

composer require maatwebsite/excel

Service Provider

Maatwebsite\Excel\ExcelServiceProvider是自动发现,并在默认情况下注册,但如果你想自己注册它:

添加ServiceProvider config/app.php

'providers' => [
    /*
     * Package Service Providers...
     */
    Maatwebsite\Excel\ExcelServiceProvider::class,
]

门面方法 Facade

Excel门面也是自动发现,但如果你想手动添加:

添加Facade :config/app.php

'aliases' => [
    ...
    'Excel' => Maatwebsite\Excel\Facades\Excel::class,
]

配置 Config

要发布配置,请运行vendor publish命令:

php artisan vendor:publish

这将创建一个名为的新配置文件config/excel.php

用法Usage

您可以通过以下方式使用Excel:

通过依赖注入:

public function __construct(\Maatwebsite\Excel\Excel $excel)
{
    $this->excel = $excel;
}

public function export()
{
    return $this->excel->export(new Export);
}

通过Exporter interface:

public function __construct(\Maatwebsite\Excel\Exporter $excel)
{
    $this->excel = $excel;
}

public function export()
{
    return $this->excel->export(new Export);
}

通过 Facade:

public function export()
{
    return Excel::export(new Export);
}

通过容器绑定:

$this->app->bind(Exporter::class, function() {
    return new Exporter($this->app['excel']);
});

更多使用方法:

2.1版本:https://laravel-excel.maatwebsite.nl/docs/2.1/getting-started/installation

3.0版本:https://laravel-excel.maatwebsite.nl/docs/3.0/getting-started/installation

更新于:2018-07-14 17:23:50
返回顶部