2017 © Pedro PelĂĄez
 

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

image

mohammed-zaki/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.

  • Tuesday, August 8, 2017
  • by mohammedzaki
  • Repository
  • 1 Watchers
  • 1 Stars
  • 774 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 12 Forks
  • 0 Open issues
  • 12 Versions
  • 22 % Grown

The README.md

Laravel Excel

Latest Stable Version Total Downloads License, (*1)

Exporting and importing Excel, CSV and OpenOffice stylesheets using Eloquent Collections and Query Builders in Laravel (5.* and 4.*).
With multiple Sheets support It's based on box/spout., (*2)

Author: Simone Todaro
Contributors: Clément Blanco, Cyber-Duck Ltd
Made with :heart: by Mohammed Zaki, (*3)

Installation
Export Excel
Export Excel with multiple sheets
Import Excel
Different formats, (*4)

Installation

Use composer to download the package:, (*5)

composer require mohammed-zaki/laravel-excel

Register the service provider in config/app.php adding CyberduckWithSheets\LaravelExcel\ExcelServiceProvider to the provider array., (*6)

Note. If you are on Laravel 4, use CyberduckWithSheets\LaravelExcel\ExcelLegacyServiceProvider, (*7)

Export Excel

Generate and download an excel file

Add, (*8)

use Exporter;

to your controller., (*9)

In your controler function, create a new excel file from an Eloquent collection., (*10)

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

The exporter class is fluent, so you can also write, (*11)

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

The exporter class supports Query builder objects as well, (*12)

$query = DB:table('table')->select('col1','col2');
$excel = Exporter::make('Excel');
$excel->loadQuery($query);
return $excel->stream($yourFileName);

Export Excel with multiple sheets

The exporter class supports Multiple Sheets as follow, (*13)

$excel = Exporter::make('Excel');
$excel->loadToNewSheet($yourCollection, "Sheet Name");
$query = DB:table('table')->select('col1','col2');
$excel->loadQueryToNewSheet($query, "Sheet Name");
return $excel->save($yourFileName);

If you deal with big tables, you can set the chunk size to minimise the memory usage, (*14)

$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

To save the excel file on the server, use the save method., (*15)

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., (*16)

To change this behaviour, create a class extending CyberduckWithSheets\LaravelExcel\Contract\SerialiserInterface, implement the methods getHeaderRow() and getData(Model $data) and set this class on the excel object usint setSerialiser()., (*17)

$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., (*18)

Example, (*19)

namespace App\Serialisers;

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

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

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

        return $row;
    }

    public function getHeaderRow()
    {
        return [
            'Field 1',
            'Field 2 (from a relationship)'
        ];
    }
}

Import Excel

Add, (*20)

use Importer;

to your controller., (*21)

In your controler function, import an excel file., (*22)

$excel = Importer::make('Excel');
$excel->load($filepath);
$collection = $excel->getCollection();
//dd($collection)

The importer class is fluent, then you can also write, (*23)

return Importer::make('Excel')->getCollection($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)., (*24)

To import a different sheet, use setSheet($sheet), (*25)

$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 CyberduckWithSheets\LaravelExcel\Contract\ParserInterface and implement the methods transform($row)., (*26)

Example, (*27)

namespace App\Parsers;

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

class ExampleSerialiser implements ParserInterface
{
    public function transform($row)
    {
        $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;
    }
}

Different formats

The package supports ODS and CSV files., (*28)

ODS

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

CSV

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

The Versions

08/08 2017

dev-master

9999999-dev

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

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent excel export importer import exporter spout

08/08 2017

v2.0.0

2.0.0.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent excel export importer import exporter spout

02/08 2017

1.2.0

1.2.0.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent excel export importer import exporter spout

01/07 2017

1.1.0

1.1.0.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent excel export importer import exporter spout

03/10 2016

1.0.7

1.0.7.0

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

  Sources   Download

MIT

The Requires

 

laravel eloquent excel export importer import exporter spout

25/09 2016

1.0.6

1.0.6.0

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

  Sources   Download

MIT

The Requires

 

laravel excel export exporter

06/09 2016

1.0.5

1.0.5.0

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

  Sources   Download

MIT

The Requires

 

laravel excel export exporter

06/09 2016

1.0.4

1.0.4.0

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

  Sources   Download

MIT

The Requires

 

laravel excel export exporter

18/04 2016

v1.0.3

1.0.3.0

  Sources   Download

MIT

The Requires

 

laravel excel

16/04 2016

v1.0.2

1.0.2.0

  Sources   Download

MIT

The Requires

 

laravel excel

16/04 2016

v1.0.1

1.0.1.0

  Sources   Download

MIT

The Requires

 

laravel excel

29/02 2016

v1.0.0

1.0.0.0

  Sources   Download

MIT

The Requires

 

laravel excel