rap2hpoutre / fast-excel
Fast Excel import/export for Laravel
Fund package maintenance!
rap2hpoutre
Installs: 13 609 835
Dependents: 55
Suggesters: 2
Security: 0
Stars: 2 104
Watchers: 30
Forks: 246
Open Issues: 83
Requires
- php: ^8.0
- illuminate/support: ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0
- openspout/openspout: ^4.24
Requires (Dev)
- illuminate/database: ^6.20.12 || ^7.30.4 || ^8.24.0 || ^9.0 || ^10.0 || ^11.0
- phpunit/phpunit: ^9.5 || ^10.1
- squizlabs/php_codesniffer: 3.*
- dev-master
- v5.5.0
- v5.4.0
- v5.3.0
- v5.2.0
- v5.1.1
- v5.1.0
- v5.0.0
- v4.1.0
- v4.0.0
- v3.2.0
- v3.1.0
- v3.0.2
- v3.0.1
- v3.0.0
- v2.5.0
- v2.4.0
- v2.3.0
- v2.2.0
- v2.1.0
- v2.0.1
- v2.0.0
- v1.7.0
- v1.6.1
- v1.6
- v1.5.0
- v1.4.0
- v1.3.2
- v1.3.1
- v1.3.0
- v1.2.2
- v1.2.1
- v1.2.0
- v1.1.0
- v1.0.0
- v0.12.0
- v0.11.0
- v0.10.1
- v0.10.0
- v0.9.0
- v0.8.0
- v0.7.0
- v0.6.0
- v0.5.0
- v0.4.0
- v0.3.2
- v0.3.1
- v0.3.0
- v0.2.2
- v0.2.1
- v0.2.0
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
- v0.0.7
- v0.0.6
- v0.0.5
- v0.0.4
- v0.0.3
- v0.0.2
- v0.0.1
- dev-analysis-RP7EZD
- dev-keep-old-phpunit
- dev-analysis-dj2m5J
- dev-analysis-16nn15
- dev-analysis-RP6YyK
- dev-analysis-322EdY
- dev-analysis-4xNrmP
- dev-rap2hpoutre-patch-1
This package is auto-updated.
Last update: 2024-11-03 08:58:44 UTC
README
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.
Still, remember that Laravel Excel has many more features.