2017 © Pedro Peláez
 

library fast-excel

image

rap2hpoutre/fast-excel

  • Wednesday, July 11, 2018
  • by rap2hpoutre
  • Repository
  • 7 Watchers
  • 199 Stars
  • 6,495 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 12 Forks
  • 5 Open issues
  • 31 Versions
  • 166 % Grown

The README.md

, (*1)

Version License StyleCI Tests Total Downloads, (*2)

Fast Excel import/export for Laravel, thanks to Spout. See benchmarks below., (*3)

Quick start

Install via composer:, (*4)

composer require rap2hpoutre/fast-excel

Export a Model to .xlsx file:, (*5)

use Rap2hpoutre\FastExcel\FastExcel;
use App\User;

// Load users
$users = User::all();

// Export all users
(new FastExcel($users))->export('file.xlsx');

Export

Export a Model or a Collection:, (*6)

$list = collect([
    [ 'id' => 1, 'name' => 'Jane' ],
    [ 'id' => 2, 'name' => 'John' ],
]);

(new FastExcel($list))->export('file.xlsx');

Export xlsx, ods and csv:, (*7)

$invoices = App\Invoice::orderBy('created_at', 'DESC')->get();
(new FastExcel($invoices))->export('invoices.csv');

Export only some attributes specifying columns names:, (*8)

(new FastExcel(User::all()))->export('users.csv', function ($user) {
    return [
        'Email' => $user->email,
        'First Name' => $user->firstname,
        'Last Name' => strtoupper($user->lastname),
    ];
});

Download (from a controller method):, (*9)

return (new FastExcel(User::all()))->download('file.xlsx');

Import

import returns a Collection:, (*10)

$collection = (new FastExcel)->import('file.xlsx');

Import a csv with specific delimiter, enclosure characters and "gbk" encoding:, (*11)

$collection = (new FastExcel)->configureCsv(';', '#', 'gbk')->import('file.csv');

Import and insert to database:, (*12)

$users = (new FastExcel)->import('file.xlsx', function ($line) {
    return User::create([
        'name' => $line['Name'],
        'email' => $line['Email']
    ]);
});

Facades

You may use FastExcel with the optional Facade. Add the following line to config/app.php under the aliases key., (*13)

'FastExcel' => Rap2hpoutre\FastExcel\Facades\FastExcel::class,

Using the Facade, you will not have access to the constructor. You may set your export data using the data method., (*14)

$list = collect([
    [ 'id' => 1, 'name' => 'Jane' ],
    [ 'id' => 2, 'name' => 'John' ],
]);

FastExcel::data($list)->export('file.xlsx');

Global helper

FastExcel provides a convenient global helper to quickly instantiate the FastExcel class anywhere in a Laravel application., (*15)

$collection = fastexcel()->import('file.xlsx');
fastexcel($collection)->export('file.xlsx');

Advanced usage

Export multiple sheets

Export multiple sheets by creating a SheetCollection:, (*16)

$sheets = new SheetCollection([
    User::all(),
    Project::all()
]);
(new FastExcel($sheets))->export('file.xlsx');

Use index to specify sheet name:, (*17)

$sheets = new SheetCollection([
    'Users' => User::all(),
    'Second sheet' => Project::all()
]);

Import multiple sheets

Import multiple sheets by using importSheets:, (*18)

$sheets = (new FastExcel)->importSheets('file.xlsx');

You can also import a specific sheet by its number:, (*19)

$users = (new FastExcel)->sheet(3)->import('file.xlsx');

Import multiple sheets with sheets names:, (*20)

$sheets = (new FastExcel)->withSheetsNames()->importSheets('file.xlsx');

Export large collections with chunk

Export rows one by one to avoid memory_limit issues using yield:, (*21)

function usersGenerator() {
    foreach (User::cursor() as $user) {
        yield $user;
    }
}

// Export consumes only a few MB, even with 10M+ rows.
(new FastExcel(usersGenerator()))->export('test.xlsx');

Add header and rows style

Add header and rows style with headerStyle and rowsStyle methods., (*22)

use OpenSpout\Common\Entity\Style\Style;

$header_style = (new Style())->setFontBold();

$rows_style = (new Style())
    ->setFontSize(15)
    ->setShouldWrapText()
    ->setBackgroundColor("EDEDED");

return (new FastExcel($list))
    ->headerStyle($header_style)
    ->rowsStyle($rows_style)
    ->download('file.xlsx');

Why?

FastExcel is intended at being Laravel-flavoured Spout: a simple, but elegant wrapper around Spout with the goal of simplifying imports and exports. It could be considered as a faster (and memory friendly) alternative to Laravel Excel, with less features. Use it only for simple tasks., (*23)

Benchmarks

Tested on a MacBook Pro 2015 2,7 GHz Intel Core i5 16 Go 1867 MHz DDR3. Testing a XLSX export for 10000 lines, 20 columns with random data, 10 iterations, 2018-04-05. Don't trust benchmarks., (*24)

Average memory peak usage Execution time
Laravel Excel 123.56 M 11.56 s
FastExcel 2.09 M 2.76 s

Still, remember that Laravel Excel has many more features., (*25)

The Versions

11/07 2018

dev-master

9999999-dev

  Sources   Download

MIT

The Requires

 

The Development Requires

by rap2h

laravel csv excel xls xlsx

10/07 2018
10/07 2018

dev-analysis-zRkn77

dev-analysis-zRkn77

  Sources   Download

MIT

The Requires

 

The Development Requires

by rap2h

laravel csv excel xls xlsx

10/07 2018
10/07 2018

dev-analysis-8QBKro

dev-analysis-8QBKro

  Sources   Download

MIT

The Requires

 

The Development Requires

by rap2h

laravel csv excel xls xlsx

06/07 2018
06/07 2018

dev-analysis-q2lddr

dev-analysis-q2lddr

  Sources   Download

MIT

The Requires

 

The Development Requires

by rap2h

laravel csv excel xls xlsx

28/05 2018
28/05 2018

dev-analysis-87YjjN

dev-analysis-87YjjN

  Sources   Download

MIT

The Requires

 

The Development Requires

by rap2h

laravel csv excel xls xlsx

27/05 2018
27/05 2018

dev-analysis-XaGBRk

dev-analysis-XaGBRk

  Sources   Download

MIT

The Requires

 

The Development Requires

by rap2h

laravel csv excel xls xlsx

27/05 2018
21/05 2018
17/05 2018

dev-rap2hpoutre-patch-1

dev-rap2hpoutre-patch-1

  Sources   Download

MIT

The Requires

 

The Development Requires

by rap2h

laravel csv excel xls xlsx

17/05 2018
17/05 2018

dev-analysis-qJO61D

dev-analysis-qJO61D

  Sources   Download

MIT

The Requires

 

The Development Requires

by rap2h

laravel csv excel xls xlsx

07/05 2018

dev-analysis-z36WJ1

dev-analysis-z36WJ1

  Sources   Download

MIT

The Requires

 

The Development Requires

by rap2h

laravel csv excel xls xlsx

07/05 2018
11/04 2018
11/04 2018
11/04 2018

dev-analysis-8LQoPK

dev-analysis-8LQoPK

  Sources   Download

MIT

The Requires

 

The Development Requires

by rap2h

laravel csv excel xls xlsx

09/04 2018
09/04 2018

v0.1.0

0.1.0.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by rap2h

laravel csv excel xls xlsx

09/04 2018

v0.0.7

0.0.7.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by rap2h

laravel csv excel xls xlsx

06/04 2018

v0.0.6

0.0.6.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by rap2h

laravel csv excel xls xlsx

05/04 2018

v0.0.5

0.0.5.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by rap2h

laravel csv excel xls xlsx

05/04 2018

v0.0.4

0.0.4.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by rap2h

laravel csv excel xls xlsx

05/04 2018

dev-scrutinizer-patch-1

dev-scrutinizer-patch-1

  Sources   Download

MIT

The Requires

 

The Development Requires

by rap2h

laravel csv excel xls xlsx

05/04 2018

v0.0.3

0.0.3.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by rap2h

laravel csv excel xls xlsx

05/04 2018

v0.0.2

0.0.2.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by rap2h

laravel csv excel xls xlsx

05/04 2018

v0.0.1

0.0.1.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by rap2h

laravel csv excel xls xlsx