• Stars
    star
    1,784
  • Rank 25,037 (Top 0.6 %)
  • Language
    PHP
  • License
    MIT License
  • Created about 6 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸ¦‰ Fast Excel import/export for Laravel

Version License StyleCI Tests Total Downloads

Fast Excel import/export for Laravel, thanks to Spout. See benchmarks below.

Quick start

Install via composer:

composer require rap2hpoutre/fast-excel

Export a Model to .xlsx file:

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:

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

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

Export xlsx, ods and csv:

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

Export only some attributes specifying columns names:

(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):

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

Import

import returns a Collection:

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

Import a csv with specific delimiter, enclosure characters and "gbk" encoding:

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

Import and insert to database:

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

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

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

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

Advanced usage

Export multiple sheets

Export multiple sheets by creating a SheetCollection:

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

Use index to specify sheet name:

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

Import multiple sheets

Import multiple sheets by using importSheets:

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

You can also import a specific sheet by its number:

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

Import multiple sheets with sheets names:

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

Export large collections with chunk

Export rows one by one to avoid memory_limit issues using yield:

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.

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.

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.

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.

More Repositories

1

laravel-log-viewer

πŸͺ Laravel log viewer
PHP
3,041
star
2

vue-picture-swipe

πŸ–Ό Vue Picture Swipe Gallery (a gallery of image with thumbnails, lazy-load and swipe) backed by photoswipe
Vue
383
star
3

pg-anonymizer

Dump anonymized PostgreSQL database with a NodeJS CLI
TypeScript
154
star
4

similar-text-finder

🐝 PHP Similar Text Finder aka Fuzzy search. `Did you mean "banana"?`
PHP
132
star
5

laravel-stripe-connect

πŸ¦“ Stripe Connect binding for Laravel
PHP
73
star
6

mysql-xplain-xplain

🐭 Explain Explainer for MySQL Queries.
PHP
53
star
7

laravel-epilog

🐈 IP, referer, user ID and more in Laravel logs
PHP
25
star
8

taskz

🐑 Sequential and parallel task list runner for terminal
JavaScript
23
star
9

laravel-prod-server

Install and run a production server for a Laravel application
22
star
10

create-user-command

πŸ¦€ Create User with artisan
PHP
21
star
11

vue-quiz

Create a quiz withΒ Vue.js
HTML
20
star
12

laravel-credit-card-validator

πŸ₯ Laravel Credit Card Validator
PHP
19
star
13

jacky

πŸ„ HTTP JSON API Client for Laravel & Lumen
PHP
17
star
14

landscape

Procedural landscape
Rust
17
star
15

llamassacre

Uncommented-spaghettiesque-dirty code. RUN AWAY.
Rust
15
star
16

remove-stop-words

Remove stop words from a string
PHP
15
star
17

htmltopdf

html to pdf website using wkhtmltopdf
Rust
9
star
18

vue-calendar

HTML
8
star
19

mongodb-anonymizer

Anonymize MongoDB database with a NodeJS CLI
TypeScript
8
star
20

uuid-rule

UUID validation rule for Laravel
PHP
7
star
21

mongodb-elasticsearch-sync

Copy data from MongoDB to Elasticsearch with a NodeJS CLI
TypeScript
6
star
22

voltaire

Proof-reading tool for PHP (spell checker, grammar fixer using LanguageTool)
PHP
6
star
23

facteur

Laravel deployer
Rust
4
star
24

nestor

🍹 Do task, rollback if something goes wrong. Just like database transactions.
PHP
4
star
25

convert-accent-characters

Converts all UTF-8 accent characters to ASCII characters
PHP
4
star
26

midikaos

🎷 Standard MIDI Files Library website
TypeScript
3
star
27

glitcher-web

🎡 WAV glitcher, generate broken beats from sample loops
TypeScript
3
star
28

csv-to-associative-array

Get your CSV as an associative array
PHP
3
star
29

ortf

Statistiques de /r/france
TypeScript
2
star
30

react-fairy-tale-emoji

JavaScript
2
star
31

fuzzy-dubstep

Jeu HTML5/JS avec Quintus (+PHP pour generation) : simulation de prΓ©paration de mariage
PHP
2
star
32

mynia

Unicorn name generator
Rust
1
star
33

drum-loop-glitcher-sonic-pi

work in progress
Ruby
1
star
34

find-the-cat

Find and click the cat.
JavaScript
1
star
35

indice

Hints for Laravel errors
Blade
1
star
36

420-bot

PHP
1
star
37

create-elasticsearch-dataset

Create a sample dataset for Elasticsearch with one command
TypeScript
1
star
38

drum-beats

Procedural Drum Beats website
Rust
1
star
39

starfield

Rust
1
star