gcordon / sheet-excel-import
为 Laravel 提供快速 Excel 导入/导出
Requires
- php: ^7.0
- box/spout: ^2.7
- illuminate/database: 5.3.* || 5.4.* || 5.5.* || 5.6.* || 5.7.* || 5.8.* || ^6.0 || ^7.0
- illuminate/support: 5.3.* || 5.4.* || 5.5.* || 5.6.* || 5.7.* || 5.8.* || ^6.0 || ^7.0
Requires (Dev)
- phpunit/phpunit: ^6.4
- dev-master
- 1.7
- v1.6.1
- 1.6.0
- 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-support-laravel-6
- dev-generator-test
- dev-analysis-8K1nW6
- dev-analysis-qgMjwk
- dev-analysis-q5erkB
- dev-analysis-Xa9dV0
- dev-analysis-zGQDMl
- dev-analysis-XpPdWa
- dev-header-style
- dev-analysis-z44Mx1
This package is not auto-updated.
Last update: 2024-09-26 17:41:52 UTC
README
Fast Excel 导入/导出为 Laravel,感谢 Spout。下面是基准测试。
快速开始
通过 composer 安装
composer require rap2hpoutre/fast-excel
将模型导出为 .xlsx
文件
use Rap2hpoutre\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(';', '#', '\n', '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' => Rap2hpoutre\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');
使用分块导出大型集合
逐行导出以避免 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
方法添加标题和行样式。
$header_style = (new StyleBuilder())->setFontBold()->build(); $rows_style = (new StyleBuilder()) ->setFontSize(15) ->setShouldWrapText() ->setBackgroundColor("EDEDED") ->build(); return (new FastExcel($list)) ->headerStyle($header_style) ->rowsStyle($style) ->download('file.xlsx');
为什么?
FastExcel 的目标是成为 Laravel 风格的 Spout:一个简单但优雅的包装,旨在简化 导入和导出。它可以被视为一个更快的(且内存友好)的替代品 Laravel Excel,功能较少。仅用于简单任务。
基准测试
在 2015 款 MacBook Pro 上测试,该电脑配备 2.7 GHz Intel Core i5 处理器,16 GB 1867 MHz DDR3 内存。测试 10000 行、20 列随机数据的 XLSX 导出,10 次迭代,2018-04-05。 不要相信基准测试。
尽管如此,请记住 Laravel Excel 具有许多更多功能。