PHPackages                             mprince/laravel-excel - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. mprince/laravel-excel

ActiveLibrary[Database &amp; ORM](/categories/database)

mprince/laravel-excel
=====================

This package provides a way to export an Eloquent collection as an excel file and to import a Excel file as an Eloquent collection.

2.2.0(5y ago)04MITPHPPHP ^7.3

Since Feb 29Pushed 4y agoCompare

[ Source](https://github.com/mprince2k18/laravel-excel-1)[ Packagist](https://packagist.org/packages/mprince/laravel-excel)[ RSS](/packages/mprince-laravel-excel/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (6)Versions (18)Used By (0)

Laravel Excel
=============

[](#laravel-excel)

[![Latest Stable Version](https://camo.githubusercontent.com/f429908de2753c73d2373c08a14307f222e89c4f4048099b7295868dc29d1993/68747470733a2f2f706f7365722e707567782e6f72672f63796265722d6475636b2f6c61726176656c2d657863656c2f762f737461626c65)](https://packagist.org/packages/cyber-duck/laravel-excel)[![Total Downloads](https://camo.githubusercontent.com/5825aaecaf94bf0e78679acad42890d25dcfa160d19ac96610140c2df577bd2e/68747470733a2f2f706f7365722e707567782e6f72672f63796265722d6475636b2f6c61726176656c2d657863656c2f646f776e6c6f616473)](https://packagist.org/packages/cyber-duck/laravel-excel)[![License](https://camo.githubusercontent.com/9f0aba68a496bee026fbb3c38c2faa1857bf4e0bc15bf9ff0d984848dc291c78/68747470733a2f2f706f7365722e707567782e6f72672f63796265722d6475636b2f6c61726176656c2d657863656c2f6c6963656e7365)](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 ❤️ by [Cyber-Duck Ltd](http://www.cyber-duck.co.uk)

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

Installation
------------

[](#installation)

Use composer to download the package:

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

```

### Laravel 4.x

[](#laravel-4x)

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

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

### Laravel &lt; 5.5

[](#laravel--55)

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

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

### Laravel &gt; 5.5

[](#laravel--55-1)

No need to register anything, since it used package auto discovery feature in Laravel 5.5.

Export Excel
------------

[](#export-excel)

### Generate and download an excel file

[](#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

```
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');
$excel->loadQuery($query);
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');
$excel->loadQuery($query);
$excel->setChunk(1000);
return $excel->stream($yourFileName);

```

### Generate and save an excel file

[](#generate-and-save-an-excel-file)

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

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

```

### Advanced usage

[](#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()*.

```
$serialiser = new CustomSerialiser();
$excel = Exporter::make('Excel');
$excel->load($collection);
$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.

Example

```
namespace App\Serialisers;

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

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

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

        return $row;
    }

    public function getHeaderRow()
    {
        return [
            'Field 1',
            '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
------------

[](#import-excel)

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

[](#advanced-usage-1)

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
-----------------

[](#different-formats)

The package supports ODS and CSV files.

### ODS

[](#ods)

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

```

### CSV

[](#csv)

```
$exporter = Exporter::make('Csv');
$importer = Importer::make('Csv');

```

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 57.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~119 days

Recently: every ~218 days

Total

16

Last Release

1938d ago

Major Versions

1.2.3 → 2.0.02020-03-26

PHP version history (3 changes)v1.0.0PHP &gt;=5.4.0

2.0.0PHP ^7.2.5

2.2.0PHP ^7.3

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/43857625?v=4)[Mohammad Prince](/maintainers/mprince2k18)[@mprince2k18](https://github.com/mprince2k18)

---

Top Contributors

[![SimoTod](https://avatars.githubusercontent.com/u/8427737?v=4)](https://github.com/SimoTod "SimoTod (22 commits)")[![clemblanco](https://avatars.githubusercontent.com/u/668419?v=4)](https://github.com/clemblanco "clemblanco (5 commits)")[![cyberduckneil](https://avatars.githubusercontent.com/u/90324469?v=4)](https://github.com/cyberduckneil "cyberduckneil (2 commits)")[![mprince2k18](https://avatars.githubusercontent.com/u/43857625?v=4)](https://github.com/mprince2k18 "mprince2k18 (2 commits)")[![joaodman](https://avatars.githubusercontent.com/u/6719189?v=4)](https://github.com/joaodman "joaodman (2 commits)")[![wsambian](https://avatars.githubusercontent.com/u/20776356?v=4)](https://github.com/wsambian "wsambian (1 commits)")[![Konafets](https://avatars.githubusercontent.com/u/363363?v=4)](https://github.com/Konafets "Konafets (1 commits)")[![matriphe](https://avatars.githubusercontent.com/u/277262?v=4)](https://github.com/matriphe "matriphe (1 commits)")[![mohammedjammeh](https://avatars.githubusercontent.com/u/41289378?v=4)](https://github.com/mohammedjammeh "mohammedjammeh (1 commits)")[![nadge](https://avatars.githubusercontent.com/u/3862793?v=4)](https://github.com/nadge "nadge (1 commits)")

---

Tags

laravelexporteloquentexcelexporterimportimporterspout

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mprince-laravel-excel/health.svg)

```
[![Health](https://phpackages.com/badges/mprince-laravel-excel/health.svg)](https://phpackages.com/packages/mprince-laravel-excel)
```

###  Alternatives

[cyber-duck/laravel-excel

This package provides a way to export an Eloquent collection as an excel file and to import a Excel file as an Eloquent collection.

74225.0k](/packages/cyber-duck-laravel-excel)[watson/validating

Eloquent model validating trait.

9723.3M47](/packages/watson-validating)[cybercog/laravel-love

Make Laravel Eloquent models reactable with any type of emotions in a minutes!

1.2k302.7k1](/packages/cybercog-laravel-love)[cviebrock/eloquent-taggable

Easy ability to tag your Eloquent models in Laravel.

567694.8k3](/packages/cviebrock-eloquent-taggable)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.2M13](/packages/reedware-laravel-relation-joins)[toponepercent/baum

Baum is an implementation of the Nested Set pattern for Eloquent models.

3154.7k](/packages/toponepercent-baum)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
