|
1 |
| -# laravel-excel |
| 1 | +# Laravel Excel |
| 2 | + |
| 3 | +[Installation](#installation) |
| 4 | +[Export Excel](#export-excel) |
| 5 | +[Import Excel](#import-excel) |
| 6 | +[Different formats](#different-formats) |
| 7 | + |
| 8 | +This package provides a way to export an Eloquent collection as an excel file and to import a Excel file as an Eloquent collection. It's based on [box/spout](https://github.com/box/spout). |
| 9 | + |
| 10 | +## Installation |
| 11 | +The package must be installed directly from [Composer](https://getcomposer.org/). |
| 12 | +Run the following command: |
| 13 | +``` |
| 14 | +$ composer require box/spout |
| 15 | +``` |
| 16 | + |
| 17 | +To make Facades available, register the service provider in config/app.php adding *Cyberduck\LaravelExcel\ExcelServiceProvider* to the provider array. |
| 18 | + |
| 19 | +Note. If you are on Laravel 4, use *Cyberduck\LaravelExcel\ExcelLegacyServiceProvider* |
| 20 | + |
| 21 | +## Export Excel |
| 22 | + |
| 23 | +### Generate and download an excel file |
| 24 | +Add |
| 25 | +``` |
| 26 | +use Exporter; |
| 27 | +``` |
| 28 | +to your controller. |
| 29 | + |
| 30 | +In your action, add |
| 31 | +``` |
| 32 | +$excel = Exporter::make('Excel'); |
| 33 | +$excel->load($yourCollection); |
| 34 | +return $excel->stream($yourFileName); |
| 35 | +``` |
| 36 | + |
| 37 | +The exporter class is fluent, then you can also write |
| 38 | +``` |
| 39 | +return Exporter::make('Excel')->load($yourCollection)->stream($yourFileName); |
| 40 | +``` |
| 41 | + |
| 42 | +### Generate and save an excel file |
| 43 | +Add |
| 44 | +``` |
| 45 | +use Exporter; |
| 46 | +``` |
| 47 | +to your controller. |
| 48 | + |
| 49 | +In your action, add |
| 50 | +``` |
| 51 | +$excel = Exporter::make('Excel'); |
| 52 | +$excel->load($yourCollection); |
| 53 | +return $excel->save($yourFileNameWithPath); |
| 54 | +``` |
| 55 | + |
| 56 | +The exporter class is fluent, then you can also write |
| 57 | +``` |
| 58 | +return Exporter::make('Excel')->load($yourCollection)->save($yourFileNameWithPath); |
| 59 | +``` |
| 60 | + |
| 61 | +### Advanced usage |
| 62 | +By default, every element of the Collection become a row and every unprotected field of the Model become a cell. No headers row is printed. |
| 63 | + |
| 64 | +To change this behaviour, create a class extending *Cyberduck\LaravelExcel\Contract\SerialiserInterface* and implement the methods *getHeaderRow()* and *getData(Model $data)*. |
| 65 | +*getHeaderRow()* must return an array of string, and every elements is a cell of the first row. To not print the header row, simply return a void array *[]*. |
| 66 | +*getData(Model $data)* must return an array of string, and every elements is a cell of rows after the header. |
| 67 | + |
| 68 | +Example |
| 69 | +``` |
| 70 | +namespace App\Serialiser; |
| 71 | +
|
| 72 | +use Illuminate\Database\Eloquent\Model; |
| 73 | +use Cyberduck\LaravelExcel\Contract\SerialiserInterface; |
| 74 | +
|
| 75 | +class ExampleSerialiser implements SerialiserInterface |
| 76 | +{ |
| 77 | + public function getData(Model $data) |
| 78 | + { |
| 79 | + $row = []; |
| 80 | +
|
| 81 | + $row[] = $data->field1; |
| 82 | + $row[] = $data->relation->field2; |
| 83 | +
|
| 84 | + return $row; |
| 85 | + } |
| 86 | +
|
| 87 | + public function getHeaderRow() |
| 88 | + { |
| 89 | + return [ |
| 90 | + 'Field 1', |
| 91 | + 'Field 2 (from a relation)' |
| 92 | + ]; |
| 93 | + } |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +## Import Excel |
| 98 | +Coming soon! (In development) |
| 99 | + |
| 100 | +## Different formats |
| 101 | +Coming soon! |
0 commit comments