155

Product support

Get help in the plugin support forum.

  • Added on Dec 5, 2016
  • Current version: 3.0.5
  • Platform Compatibility
    v3.x not tested
    v2.x use latest
    v1.x use latest
  • Created by
  • Public repository

Categories

This plugin is a wrapper for Maatwebsite/Laravel-Excel package. It adds convenient tools to import/export excel files. The complete documentation can be found at: http://www.maatwebsite.nl/laravel-excel/docs

Important!

Plugin now uses v3 of Maatwebsite/Laravel-Excel because v2 is no longer possible to use with current version of OctoberCMS (Laravel 6).

To upgrade your application code please see Upgrade guide tab on plugin page.

From Maatwebsite/Laravel-Excel documentation:

Deprecations

ALL Laravel Excel 2.* methods are deprecated and will not be able to use in 3.0 .

Excel::load() is removed and replaced by Excel::import($yourImport) Excel::create() is removed and replaced by Excel::download/Excel::store($yourExport) Excel::create()->string('xlsx') is removed an replaced by Excel:raw($yourExport, Excel::XLSX) 3.0 provides no convenience methods for styling, you are encouraged to use PhpSpreadsheets native methods.

You can find an example upgrade for an export here: https://github.com/Maatwebsite/Laravel-Excel/issues/1799

You may like my other plugins

If after installation you got any errors related to the maatwebsite/excel vendor not found: please run from your project's root

composer require maatwebsite/excel:3.1

The complete documentation can be found at: http://www.maatwebsite.nl/laravel-excel/docs

Please see the Upgrade Guide tab to understand the new code paradigm.

Example using plugin 3.x Vdomah.Excel (3.x Maatwebsite.Excel)

Example export from page code section

use Vdomah\Excel\Classes\Excel;
use Vdomah\Excel\Classes\ExportExample;
function onStart()
{
    return Excel::export(ExportExample::class, 'my_export_filename');
}

// export class
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Events\AfterSheet;

class ExportExample implements FromCollection, WithHeadings, WithEvents
{
    // set the headings
    public function headings(): array
    {
        return [
            'Company name', 'Flyer name', 'Co Company', 'Post Code', 'Online invitation', 'Pending'
        ];
    }

    // freeze the first row with headings
    public function registerEvents(): array
    {
        return [
            AfterSheet::class => function(AfterSheet $event) {
                $event->sheet->freezePane('A2', 'A2');
            },
        ];
    }

    // get the data
    public function collection()
    {
        $data = [];
// fill your export data
        return collect($data);
    }
}

Example import

// importing data somewhere, e.g. in your CMS page:
function onStart()
{
    Excel::excel()->import(new PartsImport, $filePath);
}

// import class
use Illuminate\Support\Facades\Hash;
use Maatwebsite\Excel\Concerns\ToModel;
use RainLab\User\Models\User;

class ExampleImport implements ToModel
{
    /**
     * @param array $row
     *
     * @return User|null
     */
    public function model(array $row)
    {
        // manipulate with imported data in $row; e.g. you got some $data formed;
        return User::create($data);
    }
}

New export code paradigm

Now instead of closures in v2 you need to create new class to export your data in v3. The main goal while moving from v2 to v3 is to get your export data in collection() method of your Export class (see demo using faker). Example export class is provided with plugin:

Examples using plugin 1.x Vdomah.Excel (2.x Maatwebsite.Excel)

Usage

use Vdomah\Excel\Classes\Excel;

Excel::excel()->load(base_path() . '/storage/app/media/file.xlsx', function($reader) {

    dd($reader);

});

Importing a file

To start importing a file, you can use ->load($filename). The callback is optional.

Excel::load('file.xls', function($reader) {

// Getting all results
$results = $reader->get();

// ->all() is a wrapper for ->get() and will work the same
$results = $reader->all();

});

Collections

Sheets, rows and cells are collections, this means after doing a ->get() you can use all default collection methods.

// E.g. group the results
$reader->get()->groupBy('firstname');

Getting the first sheet or row

To get the first sheet or row, you can utilise ->first().

$reader->first();
  • Found the plugin useful on 4 Apr, 2021

    Great !

  • Found the plugin useful on 6 Aug, 2018

    Useful plugin. Thank you!

  • Found the plugin useful on 6 Feb, 2018

    Another plugin that covers my basic needs, that uses the base package I'm already familiar with and have been implementing manually for some time. Easy to use and the original package documentation is helpful and works. On my internal reporting site, it gets used a ton. Thank you for providing it.

  • Found the plugin useful on 11 Sep, 2017

    Thanks for this plugin!!

    It took a short while figuring it out, a very simple example for a export using a model. It may some others some time?

    use Vdomah\Excel\Classes\Excel;

    Excel::excel()->create('File name', function($excel) {

    $excel->sheet('Sheet', function($sheet) {
        $model = Yourmodelname::get();
        $sheet->fromModel($model);
    });

    })->download('xls');

  • Found the plugin useful on 26 Jun, 2017

    https://user-images.githubusercontent.com/26702880/27474018-323efc44-582b-11e7-9980-f1b418d910c8.PNG

    But after Excel::load() code column become date format [code] => Carbon\Carbon Object ( [date] => 2017-06-23 00:00:00.000000 [timezone_type] => 3 [timezone] => UTC

  • author

    Replied on 26 Jun, 2017

    plugin is just a wrapper for 3rd party library, you need to check the logic there

  • Found the plugin useful on 18 Jun, 2017

    Works great, thanks.

3.0.5

Compatibility with PhpOffice\PhpSpreadsheet\Reader\IReader

Dec 16, 2021

3.0.4

Update for compatibility with Laravel 6

Oct 01, 2020

3.0.3

Added xlsx_styled reader type to fix error in Phpoffice/PhpSpreadsheet with custom styled .xlsx files.

May 03, 2020

3.0.2

File types restriction removed from export method. Added import shortcut method

May 03, 2020

3.0.1

!!! Migration to Maatwebsite.Laravel-Excel version 3.x.

May 23, 2019

3.0.0

Migration to Maatwebsite.Laravel-Excel version 3.x.

May 23, 2019

1.0.3

Compatibility with latest build (446 at the moment).

Feb 04, 2019

1.0.2

Class to get excel object easily.

May 08, 2017

1.0.1

Initialize plugin.

Dec 02, 2016

Upgrading from 2.x to 3.x version of Maatwebsite.Laravel-Excel.

Changes in Vdomah\Excel\Classes\Excel class
  • use of Singleton trait
  • use instance() method instead of getInstance()
  • export method added which implements Excel::download method of Maatwebsite.Laravel-Excel v3

Example export from page code section

use Vdomah\Excel\Classes\Excel;
use Vdomah\Excel\Classes\ExportExample;
function onStart()
{
    return Excel::export(ExportExample::class, 'my_export_filename');
}

New export code paradigm

Now instead of closures in v2 you need to create new class to export your data in v3. The main goal while moving from v2 to v3 is to get your export data in collection() method of your Export class (see demo using faker). Example export class is provided with plugin:

<?php namespace Vdomah\Excel\Classes;

use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Events\AfterSheet;

class ExportExample implements FromCollection, WithHeadings, WithEvents
{
    // set the headings
    public function headings(): array
    {
        return [
            'Company name', 'Flyer name', 'Co Company', 'Post Code', 'Online invitation', 'Pending'
        ];
    }

    // freeze the first row with headings
    public function registerEvents(): array
    {
        return [
            AfterSheet::class => function(AfterSheet $event) {
                $event->sheet->freezePane('A2', 'A2');
            },
        ];
    }

    // get the data
    public function collection()
    {
        $data = [];

        $faker = \Faker\Factory::create();

        $limit = 10;

        for ($i = 0; $i < $limit; $i++) {
            $data[] = [$faker->name, $faker->word, 'N', $faker->postcode, $faker->word, $faker->word];
        }

        return collect($data);
    }
}