smart145 / fast-excel
为Laravel提供的快速Excel导入/导出
资助包维护!
rap2hpoutre
Requires
- php: ^8.0
- illuminate/support: ^6.0 || ^7.0 || ^8.0 || ^9.0|^10.0
- openspout/openspout: ^4.1.1
Requires (Dev)
- illuminate/database: ^6.20.12 || ^7.30.4 || ^8.24.0 || ^9.0|^10.0
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: 3.*
- dev-master
- v3.1.4
- v3.1.3
- v3.1.2
- v3.1.0
- v3.0.0
- v2.0.0
- v1.2.10
- v1.2.9
- v1.2.8
- v1.2.7
- v1.2.6
- v1.2.5
- v1.2.4
- v1.2.3
- 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-dont_show_columns_with_underscore
- dev-fix_error_export
- dev-fix_error_collection
- dev-feature/restric_columns_with_for_those_started_with_underscore
- dev-analysis-XpPdWa
- dev-header-style
- dev-analysis-z44Mx1
This package is auto-updated.
Last update: 2024-09-24 19:20:33 UTC
README
Fast Excel为Laravel提供的快速导入/导出,感谢Spout。请参见下方的基准测试。
快速开始
使用composer安装
composer require smart145/fast-excel
将模型导出到.xlsx
文件
use Smart145\FastExcel\FastExcel; use App\User; // Load users $users = User::all(); // Export all users (new FastExcel($users))->export('file.xlsx');
导出
导出模型或一个集合
$list = collect([ [ 'id' => 1, 'name' => 'Jane' ], [ 'id' => 2, 'name' => 'John' ], ]); (new FastExcel($list))->export('file.xlsx');
导出xlsx
、ods
和csv
$invoices = App\Invoice::orderBy('created_at', 'DESC')->get(); (new FastExcel($invoices))->export('invoices.csv');
通过指定列名只导出某些属性
(new FastExcel(User::all()))->export('users.csv', function ($user) { return [ 'Email' => $user->email, 'First Name' => $user->firstname, 'Last Name' => strtoupper($user->lastname), ]; });
下载(从控制器方法中)
return (new FastExcel(User::all()))->download('file.xlsx');
导入
import
返回一个集合
$collection = (new FastExcel)->import('file.xlsx');
导入具有特定分隔符、包围字符和“gbk”编码的csv
$collection = (new FastExcel)->configureCsv(';', '#', 'gbk')->import('file.csv');
导入并插入到数据库
$users = (new FastExcel)->import('file.xlsx', function ($line) { return User::create([ 'name' => $line['Name'], 'email' => $line['Email'] ]); });
门面
您可以使用可选的门面来使用FastExcel。将以下行添加到config/app.php
中的aliases
键下。
'FastExcel' => Smart145\FastExcel\Facades\FastExcel::class,
使用门面,您将无法访问构造函数。您可以使用data
方法设置您的导出数据。
$list = collect([ [ 'id' => 1, 'name' => 'Jane' ], [ 'id' => 2, 'name' => 'John' ], ]); FastExcel::data($list)->export('file.xlsx');
全局助手
FastExcel提供了一个方便的全局助手,可以在Laravel应用程序的任何地方快速实例化FastExcel类。
$collection = fastexcel()->import('file.xlsx'); fastexcel($collection)->export('file.xlsx');
高级使用
导出多个工作表
通过创建一个SheetCollection
来导出多个工作表
$sheets = new SheetCollection([ User::all(), Project::all() ]); (new FastExcel($sheets))->export('file.xlsx');
使用索引来指定工作表名称
$sheets = new SheetCollection([ 'Users' => User::all(), 'Second sheet' => Project::all() ]);
导入多个工作表
通过使用importSheets
导入多个工作表
$sheets = (new FastExcel)->importSheets('file.xlsx');
您也可以通过其编号导入特定的工作表
$users = (new FastExcel)->sheet(3)->import('file.xlsx');
通过工作表名称导入多个工作表
$sheets = (new FastExcel)->withSheetsNames()->importSheets('file.xlsx');
使用块导出大型集合
逐行导出以避免memory_limit
问题 使用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');
添加标题和行样式
使用headerStyle
和rowsStyle
方法添加标题和行样式。
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');
为什么?
FastExcel旨在成为Laravel风格的Spout:一个简单但优雅的包装,旨在简化导入和导出。它可以被认为是Laravel Excel的更快速(且内存友好)的替代品,功能较少。仅用于简单任务。
基准测试
在2015年2.7 GHz Intel Core i5 16 Go 1867 MHz DDR3的MacBook Pro上测试。测试了10000行,20列,随机数据,XLSX导出,10次迭代,2018-04-05。 不要相信基准测试。
然而,请记住,Laravel Excel 有更多的功能。