Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":1,"defects":{"ExporterTest::test_can_export_csv":4,"ExporterTest::test_can_export_ods":4,"ExporterTest::test_can_export_xlsx":4,"ExporterTest::test_can_use_a_query_builder":4,"ExporterTest::test_can_use_a_query_builder_with_chunk":4,"ExporterTest::test_can_use_a_custom_serialiser":4,"ImporterFactoryTest::test_factory_can_create_csv":4,"ImporterFactoryTest::test_factory_can_create_odt":4,"ImporterFactoryTest::test_factory_can_create_xls":4,"ExcelServiceProviderTest::test_service_provider":4},"times":{"ExporterTest::test_can_export_csv":0.101,"ExporterTest::test_can_export_ods":0.009,"ExporterTest::test_can_export_xlsx":0.008,"ExporterTest::test_can_use_a_query_builder":0.009,"ExporterTest::test_can_use_a_query_builder_with_chunk":0.01,"ExporterTest::test_can_use_a_custom_serialiser":0.008,"ExporterFacadeTest::test_facades_are_available":0.028,"ImporterFactoryTest::test_factory_can_create_csv":0.009,"ImporterFactoryTest::test_factory_can_create_odt":0.009,"ImporterFactoryTest::test_factory_can_create_xls":0.01,"ImporterFactoryTest::test_exporter_factory_can_create_csv":0.01,"ImporterFactoryTest::test_exporter_factory_can_create_odt":0.01,"ImporterFactoryTest::test_exporter_factory_can_create_xls":0.01,"ExcelServiceProviderTest::test_service_provider":0.009}}
55 changes: 42 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# 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.*).
Exporting and importing Excel, CSV and OpenOffice stylesheets using Eloquent Collections and Query Builders in Laravel (6._, 7._ 8._and 9._).
It's based on [box/spout](https://github.com/box/spout).

Author: [Simone Todaro](https://github.com/SimoTod)
Expand All @@ -13,12 +13,14 @@ 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)
[Different formats](#different-formats)

## Installation

Use composer to download the package:

```
composer require cyber-duck/laravel-excel
composer require jodeveloper/laravel-excel
```

### Laravel 4.x
Expand All @@ -27,7 +29,7 @@ Register the service provider in `config/app.php` by adding this line to provide

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

Expand All @@ -37,7 +39,7 @@ Register the service provider in `config/app.php` by adding this line to provide

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

Expand All @@ -48,25 +50,31 @@ No need to register anything, since it used package auto discovery feature in La
## Export Excel

### Generate and download an excel file

Add

```
use Exporter;
```

to your controller.

In your controler function, create a new excel file from an Eloquent collection.

```
$excel = Exporter::make('Excel');
$excel->load($yourCollection);
return $excel->stream($yourFileName);
```

The exporter class is fluent, so you can also write
The exporter class is fluent, so you can also write

```
return Exporter::make('Excel')->load($yourCollection)->stream($yourFileName);
```

The exporter class supports Query builder objects as well

```
$query = DB:table('table')->select('col1','col2');
$excel = Exporter::make('Excel');
Expand All @@ -75,6 +83,7 @@ return $excel->stream($yourFileName);
```

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');
Expand All @@ -84,16 +93,20 @@ return $excel->stream($yourFileName);
```

### Generate and save an excel file

To save the excel file on the server, use the save method.

```
return $excel->save($yourFileNameWithPath);
```

### Advanced usage

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()*.
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');
Expand All @@ -102,10 +115,11 @@ $excel->setSerialiser($serialiser);
return $excel->stream($yourFileName);
```

*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.
_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\Serialisers;

Expand Down Expand Up @@ -133,19 +147,25 @@ class ExampleSerialiser implements SerialiserInterface
}
}
```

then set the serialiser before saving the file the collection.

```
$collection = Exporter::make('Excel')->load($yourCollection)->setSerialiser(new ExampleSerialiser)->stream($yourFileName);
```

## Import Excel

Add

```
use Importer;
```

to your controller.

In your controler function, import an excel file.

```
$excel = Importer::make('Excel');
$excel->load($filepath);
Expand All @@ -154,14 +174,17 @@ $collection = $excel->getCollection();
```

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)*
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);
Expand All @@ -170,9 +193,10 @@ $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)*.
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;

Expand All @@ -192,21 +216,26 @@ class ExampleParser implements ParserInterface
}
}
```

then set the parser before creating the collection.

```
$collection = Importer::make('Excel')->load($filepath)->setParser(new ExampleParser)->getCollection();
```

## Different formats

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');
Expand Down
99 changes: 54 additions & 45 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,47 +1,56 @@
{
"name": "cyber-duck/laravel-excel",
"type": "library",
"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": "Cyber-Duck",
"email": "[email protected]",
"role": "Developer"
}
],
"require": {
"php": "^7.3|^8.0",
"box/spout": "^3.1",
"illuminate/database": "^6.0.0|^7.0.0|^8.0.0",
"illuminate/support": "^6.0.0|^7.0.0|^8.0.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5.0",
"phpspec/phpspec": "^7.0.0",
"laravel/laravel": "^6.0.0|^7.0.0|^8.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"
"name": "jodeveloper/laravel-excel",
"type": "library",
"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": "jodeveloper",
"email": "[email protected]",
"role": "Developer"
}
],
"require": {
"php": "^7.3|^8.0",
"box/spout": "^3.2",
"illuminate/database": "^6.0.0|^7.0.0|^8.0.0|^9",
"illuminate/support": "^6.0.0|^7.0.0|^8.0.0|^9"
},
"require-dev": {
"phpunit/phpunit": "^9.5.0",
"phpspec/phpspec": "^7.0.0",
"laravel/laravel": "^6.0.0|^7.0.0|^8.0.0|^9"
},
"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"
}