# Installation

# Requirements

  • PHP: ^7.0
  • Laravel: ^5.5
  • PhpSpreadsheet: ^1.2
  • PHP extension php_zip enabled
  • PHP extension php_xml enabled
  • PHP extension php_gd2 enabled

# Installation

Require this package in the composer.json of your Laravel project. This will download the package and PhpSpreadsheet.

composer require maatwebsite/excel
1

The Maatwebsite\Excel\ExcelServiceProvider is auto-discovered and registered by default, but if you want to register it yourself:

Add the ServiceProvider in config/app.php:

'providers' => [
    /*
     * Package Service Providers...
     */
    Maatwebsite\Excel\ExcelServiceProvider::class,
]
1
2
3
4
5
6

The Excel facade is also auto-discovered, but if you want to add it manually:

Add the Facade in config/app.php:

'aliases' => [
    ...
    'Excel' => Maatwebsite\Excel\Facades\Excel::class,
]
1
2
3
4

To publish the config, run the vendor publish command:

php artisan vendor:publish
1

This will create a new config file named config/excel.php.

# Usage

Laravel Excel can be used in a various of ways. I'm sure you will find your prefered of using it. This can be either via dependency injection or if you prefer you can even use a facade.

# Via dependency injection

You can inject the Excel manager class into your class, either via constructor injection or method injection in case of a controller.

use App\YourExport;
use Maatwebsite\Excel\Excel;

class YourController
{
    private $excel;

    public function __construct(Excel $excel)
    {
        $this->excel = $excel;
    }
    
    public function export()
    {
        return $this->excel->download(new YourExport);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# Via Exporter interface

You can also use the Maatwebsite\Excel\Exporter to decouple more from the concrete Excel manager implementation.

use App\YourExport;
use Maatwebsite\Excel\Exporter;

class YourController
{
    private $exporter;

    public function __construct(Exporter $exporter)
    {
        $this->exporter = $exporter;
    }
    
    public function export()
    {
        return $this->exporter->download(new YourExport);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# Via the Facade

If you prefer facades, you can you use the Maatwebsite\Excel\Facades\Excel facade or if you use the alias the Excel facade alias.

use App\YourExport;
use Maatwebsite\Excel\Facades\Excel;

class YourController
{
    public function export()
    {
        return Excel::download(new YourExport);
    }
}
1
2
3
4
5
6
7
8
9
10

# Via container binding

If you want to bind the Excel manager to your own class via a container binding, you can use the excel container binding.

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