Skip to content

Commit 73fcd16

Browse files
committed
Update documentation
1 parent 2fde479 commit 73fcd16

File tree

5 files changed

+101
-19
lines changed

5 files changed

+101
-19
lines changed

README.md

Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
# Laravel Excel
22

3+
[![Latest Stable Version](https://poser.pugx.org/cyber-duck/laravel-excel/v/stable)](https://packagist.org/packages/cyber-duck/laravel-excel)
4+
[![Total Downloads](https://poser.pugx.org/cyber-duck/laravel-excel/downloads)](https://packagist.org/packages/cyber-duck/laravel-excel)
5+
[![License](https://poser.pugx.org/cyber-duck/laravel-excel/license)](https://raw.githubusercontent.com/Cyber-Duck/laravel-excel/master/LICENSE)
6+
7+
Laravel Excel is a pachage to export and import excel files using Eloquent Collections in Laravel (5.* and 4.*).
8+
It's based on [box/spout](https://github.com/box/spout).
9+
10+
Author: [Simone Todaro](https://github.com/SimoTod)
11+
12+
Made with :heart: by [Cyber-Duck Ltd](http://www.cyber-duck.co.uk)
13+
314
[Installation](#installation)
415
[Export Excel](#export-excel)
516
[Import Excel](#import-excel)
617
[Different formats](#different-formats)
718

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-
1019
## Installation
1120
```
1221
$ composer require cyber-duck/laravel-excel
@@ -25,7 +34,7 @@ use Exporter;
2534
```
2635
to your controller.
2736

28-
In your action, add
37+
In your controler function, create a new excel file.
2938
```
3039
$excel = Exporter::make('Excel');
3140
$excel->load($yourCollection);
@@ -44,7 +53,7 @@ use Exporter;
4453
```
4554
to your controller.
4655

47-
In your action, add
56+
In your controler function, create a new excel file.
4857
```
4958
$excel = Exporter::make('Excel');
5059
$excel->load($yourCollection);
@@ -66,7 +75,7 @@ To change this behaviour, create a class which extends *Cyberduck\LaravelExcel\C
6675

6776
Example
6877
```
69-
namespace App\Serialiser;
78+
namespace App\Serialisers;
7079
7180
use Illuminate\Database\Eloquent\Model;
7281
use Cyberduck\LaravelExcel\Contract\SerialiserInterface;
@@ -94,7 +103,72 @@ class ExampleSerialiser implements SerialiserInterface
94103
```
95104

96105
## 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
121+
```
122+
return Importer::make('Excel')->getCollection($filepath)->getCollection();
123+
```
124+
125+
### Advanced usage
126+
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
154+
$model->field3 = new \Carbon($row[2]);
155+
return $model;
156+
}
157+
}
158+
```
98159

99160
## Different formats
100-
Coming soon!
161+
The pachage supports ODS and CSV files.
162+
163+
### ODS
164+
```
165+
$exporter = Exporter::make('OpenOffice');
166+
$importer = Importer::make('OpenOffice');
167+
```
168+
169+
### CSV
170+
```
171+
$exporter = Exporter::make('Csv');
172+
$importer = Importer::make('Csv');
173+
```
174+

composer.json

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,25 @@
22
"name": "cyber-duck/laravel-excel",
33
"type": "library",
44
"description": "This package provides a way to export an Eloquent collection as an excel file and to import a Excel file as an Eloquent collection.",
5-
"keywords": ["laravel", "excel", "exporter", "export"],
6-
"require": {
7-
"php": ">=5.4.0",
8-
"box/spout": "^2.4",
9-
"illuminate/database": "4.*|5.*",
10-
"illuminate/support": "4.*|5.*"
11-
},
5+
"keywords": ["laravel", "excel", "exporter", "export", "importer", "import", "eloquent", "spout"],
126
"license": "MIT",
137
"authors": [
148
{
159
"name": "Simone Todaro",
16-
"email": "[email protected]",
10+
"email": "[email protected]",
1711
"role": "Developer"
1812
}
1913
],
14+
"require": {
15+
"php": ">=5.4.0",
16+
"box/spout": "^2.4",
17+
"illuminate/database": "4.*|5.*",
18+
"illuminate/support": "4.*|5.*"
19+
},
2020
"autoload": {
2121
"psr-4": {
2222
"Cyberduck\\LaravelExcel\\": "src"
2323
}
24-
}
24+
},
25+
"minimum-stability": "stable"
2526
}

src/Contract/ParserInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
namespace Cyberduck\LaravelExcel\Contract;
3+
4+
interface ParserInterface
5+
{
6+
public function transform($array);
7+
}

src/Importer/AbstractSpreadsheet.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ protected function parseRows($reader)
6161
continue;
6262
}
6363
foreach ($sheet->getRowIterator() as $row) {
64-
$collection[] = $this->parser->getData($row);
64+
$collection[] = $this->parser->transform($row);
6565
}
6666
}
6767
return collect($collection);

src/Parser/BasicParser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?php
22
namespace Cyberduck\LaravelExcel\Parser;
33

4-
use Illuminate\Database\Eloquent\Model;
4+
use Cyberduck\LaravelExcel\Contract\ParserInterface;
55

66
class BasicParser implements ParserInterface
77
{
8-
public function getData($row)
8+
public function transform($row)
99
{
1010
return $row;
1111
}

0 commit comments

Comments
 (0)