You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Made with :heart: by [Cyber-Duck Ltd](http://www.cyber-duck.co.uk)
13
+
3
14
[Installation](#installation)
4
15
[Export Excel](#export-excel)
5
16
[Import Excel](#import-excel)
6
17
[Different formats](#different-formats)
7
18
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
19
## Installation
11
20
```
12
21
$ composer require cyber-duck/laravel-excel
@@ -25,7 +34,7 @@ use Exporter;
25
34
```
26
35
to your controller.
27
36
28
-
In your action, add
37
+
In your controler function, create a new excel file.
29
38
```
30
39
$excel = Exporter::make('Excel');
31
40
$excel->load($yourCollection);
@@ -44,7 +53,7 @@ use Exporter;
44
53
```
45
54
to your controller.
46
55
47
-
In your action, add
56
+
In your controler function, create a new excel file.
48
57
```
49
58
$excel = Exporter::make('Excel');
50
59
$excel->load($yourCollection);
@@ -66,7 +75,7 @@ To change this behaviour, create a class which extends *Cyberduck\LaravelExcel\C
66
75
67
76
Example
68
77
```
69
-
namespace App\Serialiser;
78
+
namespace App\Serialisers;
70
79
71
80
use Illuminate\Database\Eloquent\Model;
72
81
use Cyberduck\LaravelExcel\Contract\SerialiserInterface;
@@ -94,7 +103,72 @@ class ExampleSerialiser implements SerialiserInterface
94
103
```
95
104
96
105
## Import Excel
97
-
Coming soon! (In development)
106
+
Add
107
+
```
108
+
use Importer;
109
+
```
110
+
to your controller.
111
+
112
+
In your controler function, import an excel file.
113
+
```
114
+
$excel = Importer::make('Excel');
115
+
$excel->load($filepath);
116
+
$collection = $excel->getCollection();
117
+
//dd($collection)
118
+
```
119
+
120
+
The importer class is fluent, then you can also write
By default, every row of the first sheet of the excel file becomes an array and the final result is wraped in a Collection (Illuminate\Support\Collection).
127
+
128
+
To import a different sheet, use *setSheet($sheet)*
129
+
```
130
+
$excel = Importer::make('Excel');
131
+
$excel->load($filepath);
132
+
$excel->setSheet($sheetNumber);
133
+
$collection = $excel->getCollection();
134
+
//dd($collection)
135
+
```
136
+
137
+
To import each row in an Eloquent model, create a class which extends *Cyberduck\LaravelExcel\Contract\ParserInterface* and implements the methods *transform($row)*.
138
+
139
+
Example
140
+
```
141
+
namespace App\Parsers;
142
+
143
+
use App\Models\YourModel;
144
+
use Cyberduck\LaravelExcel\Contract\ParserInterface;
145
+
146
+
class ExampleSerialiser implements ParserInterface
147
+
{
148
+
public function transform($row)
149
+
{
150
+
$model = new YourModel();
151
+
$model->field1 = $row[0];
152
+
$model->field2 = $row[1];
153
+
// We can manunipulate the data before returning the object
0 commit comments