Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/laravel 7 support #1

Open
wants to merge 33 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
94b92bb
Update ImporterInterface.php
clemblanco Apr 18, 2016
c028422
Update composer.json
clemblanco Sep 6, 2016
1801e17
Update README.md
clemblanco Sep 6, 2016
f7421f0
Update composer.json
clemblanco Sep 6, 2016
b8f5d15
Update composer.json
clemblanco Sep 6, 2016
7d9f218
Update composer.json
SimoTod Sep 25, 2016
2fde479
Update README.md
SimoTod Sep 25, 2016
73fcd16
Update documentation
SimoTod Oct 2, 2016
df59992
Update README.md
SimoTod Feb 3, 2017
812df64
Add support for query builder objects and generic collections
SimoTod Jun 28, 2017
46f9a91
Add tests
SimoTod Jun 28, 2017
e63b71d
Update readme
SimoTod Jun 30, 2017
efc14d0
Fix importer issue
SimoTod Aug 1, 2017
5e7de01
Refactor importer
Aug 2, 2017
fc41ddd
Fix undefined property
Aug 2, 2017
8cfbf3b
Skip row when parser returns false
SimoTod Aug 27, 2017
33eccf7
Add proxy __call methods to access the underlying Spout functions
Sep 4, 2017
cbfea84
Merge branch 'master' of https://github.com/Cyber-Duck/laravel-excel
Sep 4, 2017
7afd016
Add support for relationships
SimoTod Sep 11, 2017
1cbf687
Update AbstractSpreadsheet.php
SimoTod Sep 11, 2017
2d42306
Update AbstractSpreadsheet.php
SimoTod Sep 11, 2017
04a3462
Add package auto discovery feature support for Laravel 5.5
matriphe Oct 6, 2017
0459b7c
Update README.md
wsambian Oct 26, 2017
0189f49
Make the Importer fluent
Konafets Dec 4, 2017
c059429
Update README.md
SimoTod Aug 31, 2018
7939f48
Merge pull request #2 from matriphe/add-package-auto-discovery
SimoTod Aug 31, 2018
a3274a6
Merge pull request #6 from Konafets/feature/MakeImporterFluent
SimoTod Aug 31, 2018
8ddc542
Merge pull request #4 from wsambian/patch-1
SimoTod Aug 31, 2018
d9f8d70
Update README.md
SimoTod Aug 31, 2018
7b74781
This package and its dependencies has been upgraded to use laravel 6.
Mar 26, 2020
c52bec6
Merge pull request #12 from mohammedjammeh/feature/upgrade-to-laravel-6
joaodman Mar 26, 2020
e323223
Added laravel 7 to supported versions
cyberduckneil Mar 30, 2020
8dc4cbe
Remove mistake from composer file
cyberduckneil Mar 30, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
184 changes: 148 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,85 +1,125 @@
# Laravel Excel
# Laravel Excel
[![Latest Stable Version](https://poser.pugx.org/cyber-duck/laravel-excel/v/stable)](https://packagist.org/packages/cyber-duck/laravel-excel)
[![Total Downloads](https://poser.pugx.org/cyber-duck/laravel-excel/downloads)](https://packagist.org/packages/cyber-duck/laravel-excel)
[![License](https://poser.pugx.org/cyber-duck/laravel-excel/license)](https://raw.githubusercontent.com/Cyber-Duck/laravel-excel/master/LICENSE)

Exporting and importing Excel, CSV and OpenOffice stylesheets using Eloquent Collections and Query Builders in Laravel (5.* and 4.*).
It's based on [box/spout](https://github.com/box/spout).

Author: [Simone Todaro](https://github.com/SimoTod)
Contributors: [Clément Blanco](https://github.com/Claymm)
Made with :heart: by [Cyber-Duck Ltd](http://www.cyber-duck.co.uk)

[Installation](#installation)
[Export Excel](#export-excel)
[Import Excel](#import-excel)
[Different formats](#different-formats)

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).
## Installation
Use composer to download the package:
```
composer require cyber-duck/laravel-excel
```

### Laravel 4.x

Register the service provider in `config/app.php` by adding this line to providers array.

## Installation
The package must be installed directly from [Composer](https://getcomposer.org/).
Run the following command:
```php
'providers' => [
Cyberduck\LaravelExcel\ExcelLegacyServiceProvider::class,
],
```
$ composer require cyberduck/laravel-excel

### Laravel < 5.5

Register the service provider in `config/app.php` by adding this line to providers array.

```php
'providers' => [
Cyberduck\LaravelExcel\ExcelServiceProvider::class,
],
```

To make Facades available, register the service provider in config/app.php adding *Cyberduck\LaravelExcel\ExcelServiceProvider* to the provider array.
### Laravel 5.5

Note. If you are on Laravel 4, use *Cyberduck\LaravelExcel\ExcelLegacyServiceProvider*
No need to register anything, since it used package auto discovery feature in Laravel 5.5.

## Export Excel

### Generate and download an excel file
Add
Add
```
use Exporter;
```
```
to your controller.

In your action, add
In your controler function, create a new excel file from an Eloquent collection.
```
$excel = Exporter::make('Excel');
$excel->load($yourCollection);
return $excel->stream($yourFileName);
```
$excel->load($yourCollection);
return $excel->stream($yourFileName);
```

The exporter class is fluent, then you can also write
The exporter class is fluent, so you can also write
```
return Exporter::make('Excel')->load($yourCollection)->stream($yourFileName);
```

### Generate and save an excel file
Add
The exporter class supports Query builder objects as well
```
$query = DB:table('table')->select('col1','col2');
$excel = Exporter::make('Excel');
$excel->loadQuery($query);
return $excel->stream($yourFileName);
```
use Exporter;
```
to your controller.

In your action, add
If you deal with big tables, you can set the chunk size to minimise the memory usage
```
$query = DB:table('table')->select('col1','col2');
$excel = Exporter::make('Excel');
$excel->load($yourCollection);
return $excel->save($yourFileNameWithPath);
```
$excel->loadQuery($query);
$excel->setChunk(1000);
return $excel->stream($yourFileName);
```

The exporter class is fluent, then you can also write
### Generate and save an excel file
To save the excel file on the server, use the save method.
```
return Exporter::make('Excel')->load($yourCollection)->save($yourFileNameWithPath);
return $excel->save($yourFileNameWithPath);
```

### Advanced usage
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.
By default, every element of the Collection becomes a row and every unprotected field of the Model becomes a cell.
No headers row is printed.

To change this behaviour, create a class extending *Cyberduck\LaravelExcel\Contract\SerialiserInterface*, implement the methods *getHeaderRow()* and *getData(Model $data)* and set this class on the excel object usint *setSerialiser()*.
```
$serialiser = new CustomSerialiser();
$excel = Exporter::make('Excel');
$excel->load($collection);
$excel->setSerialiser($serialiser);
return $excel->stream($yourFileName);
```

To change this behaviour, create a class extending *Cyberduck\LaravelExcel\Contract\SerialiserInterface* and implement the methods *getHeaderRow()* and *getData(Model $data)*.
*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 *[]*.
*getData(Model $data)* must return an array of string, and every elements is a cell of rows after the header.
*getHeaderRow()* must return an array of string where every element is a cell of the first row. To not print the header row, simply return a void array *[]*.
*getData(Model $data)* must return an array of string, and every elements is a cell.

Example
```
namespace App\Serialiser;
namespace App\Serialisers;

use Illuminate\Database\Eloquent\Model;
use Cyberduck\LaravelExcel\Contract\SerialiserInterface;

class ExampleSerialiser implements SerialiserInterface
{
public function getData(Model $data)
public function getData($data)
{
$row = [];

$row[] = $data->field1;
$row[] = $data->relation->field2;
$row[] = $data->relationship->field2;

return $row;
}
Expand All @@ -88,14 +128,86 @@ class ExampleSerialiser implements SerialiserInterface
{
return [
'Field 1',
'Field 2 (from a relation)'
'Field 2 (from a relationship)'
];
}
}
```
then set the serialiser before saving the file the collection.
```
$collection = Exporter::make('Excel')->load($yourCollection)->setSerialiser(new ExampleSerialiser)->stream($yourFileName);
```

## Import Excel
Coming soon! (In development)
Add
```
use Importer;
```
to your controller.

In your controler function, import an excel file.
```
$excel = Importer::make('Excel');
$excel->load($filepath);
$collection = $excel->getCollection();
//dd($collection)
```

The importer class is fluent, then you can also write
```
return Importer::make('Excel')->load($filepath)->getCollection();
```

### Advanced usage
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).

To import a different sheet, use *setSheet($sheet)*
```
$excel = Importer::make('Excel');
$excel->load($filepath);
$excel->setSheet($sheetNumber);
$collection = $excel->getCollection();
//dd($collection)
```

To import each row in an Eloquent model, create a class extending *Cyberduck\LaravelExcel\Contract\ParserInterface* and implement the methods *transform($row)*.

Example
```
namespace App\Parsers;

use App\Models\YourModel;
use Cyberduck\LaravelExcel\Contract\ParserInterface;

class ExampleParser implements ParserInterface
{
public function transform($row, $header)
{
$model = new YourModel();
$model->field1 = $row[0];
$model->field2 = $row[1];
// We can manunipulate the data before returning the object
$model->field3 = new \Carbon($row[2]);
return $model;
}
}
```
then set the parser before creating the collection.
```
$collection = Importer::make('Excel')->load($filepath)->setParser(new ExampleParser)->getCollection();
```

## Different formats
Coming soon!
The package supports ODS and CSV files.

### ODS
```
$exporter = Exporter::make('OpenOffice');
$importer = Importer::make('OpenOffice');
```

### CSV
```
$exporter = Exporter::make('Csv');
$importer = Importer::make('Csv');
```
44 changes: 33 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,47 @@
{
"name": "cyberduck/laravel-excel",
"name": "cyber-duck/laravel-excel",
"type": "library",
"description": "",
"keywords": ["Laravel", "Excel"],
"require": {
"php": ">=5.4.0",
"box/spout": "^2.4",
"illuminate/database": "4.*|5.1",
"illuminate/support": "4.*|5.1"
},
"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.",
"keywords": ["laravel", "excel", "exporter", "export", "importer", "import", "eloquent", "spout"],
"license": "MIT",
"authors": [
{
"name": "Simone Todaro",
"email": "simone@cyber-duck.co.uk",
"email": "simo.todaro@gmail.com",
"role": "Developer"
}
],
"require": {
"php": "^7.2.5",
"box/spout": "^3.1",
"illuminate/database": "^6.0.0|^7.0.0",
"illuminate/support": "^6.0.0|^7.0.0"
},
"require-dev": {
"phpunit/phpunit": "^7.0",
"phpspec/phpspec": "^6.1.1",
"laravel/laravel": "^6.0.0|^7.0.0"
},
"autoload": {
"psr-4": {
"Cyberduck\\LaravelExcel\\": "src"
}
}
},
"autoload-dev": {
"files": [
"tests/TestCase.php",
"tests/utils/Item.php",
"tests/utils/Migration.php",
"tests/utils/DatabaseSeeder.php",
"tests/utils/FirstColumnOnlySerialiser.php"
]
},
"extra": {
"laravel": {
"providers": [
"Cyberduck\\LaravelExcel\\ExcelServiceProvider"
]
}
},
"minimum-stability": "stable"
}
22 changes: 22 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
>
<php>
<ini name="display_errors" value="On" />
<ini name="display_startup_errors" value="On" />
</php>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
<exclude>./tests/views</exclude>
</testsuite>
</testsuites>
</phpunit>
5 changes: 4 additions & 1 deletion src/Contract/ExporterInterface.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<?php
namespace Cyberduck\LaravelExcel\Contract;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Collection;
use Illuminate\Database\Query\Builder;

interface ExporterInterface
{
public function load(Collection $data);
public function loadQuery(Builder $query);
public function setChunk($size);
public function setSerialiser(SerialiserInterface $serialiser);
public function save($filename);
public function stream($filename);
Expand Down
7 changes: 5 additions & 2 deletions src/Contract/ImporterInterface.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<?php
namespace Cyberduck\LaravelExcel\Contract;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

interface ExporterInterface
interface ImporterInterface
{
public function load($path);
public function setParser(ParserInterface $parser);
public function setModel(Model $model);
public function setSheet($sheet);
public function getCollection();
public function save($updateIfEquals);
}
7 changes: 7 additions & 0 deletions src/Contract/ParserInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace Cyberduck\LaravelExcel\Contract;

interface ParserInterface
{
public function transform($array, $header);
}
4 changes: 1 addition & 3 deletions src/Contract/SerialiserInterface.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<?php
namespace Cyberduck\LaravelExcel\Contract;

use Illuminate\Database\Eloquent\Model;

interface SerialiserInterface
{
public function getData(Model $data);
public function getData($data);
public function getHeaderRow();
}
4 changes: 2 additions & 2 deletions src/ExcelServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class ExcelServiceProvider extends ServiceProvider
public function boot()
{
$loader = AliasLoader::getInstance();
$loader->alias('Exporter', '\Cyberduck\LaravelExcel\ExporterFacade');
$loader->alias('Importer', '\Cyberduck\LaravelExcel\ImporterFacade');
$loader->alias('Exporter', \Cyberduck\LaravelExcel\ExporterFacade::class);
$loader->alias('Importer', \Cyberduck\LaravelExcel\ImporterFacade::class);
}

public function register()
Expand Down
Loading