FastExcel使用教程:Laravel高效Excel处理技巧
FastExcel是一个专为Laravel框架设计的快速Excel导入导出库,基于Spout库开发,提供了比传统方案更快且更节省内存的解决方案。本教程将详细介绍FastExcel的核心功能和实际应用。
项目介绍
FastExcel旨在为Laravel应用程序提供简单但优雅的Excel导入导出功能。它是Spout库的Laravel风格包装器,专注于简化数据导入导出操作,可作为Laravel Excel的更快且内存友好的替代方案。
快速开始
安装FastExcel
通过Composer安装FastExcel:
composer require rap2hpoutre/fast-excel
基本导出功能
将模型数据导出到Excel文件:
use Rap2hpoutre\FastExcel\FastExcel;
use App\User;
// 加载用户数据
$users = User::all();
// 导出所有用户数据
(new FastExcel($users))->export('file.xlsx');
集合数据导出
导出集合数据到Excel:
$list = collect([
[ 'id' => 1, 'name' => 'Jane' ],
[ 'id' => 2, 'name' => 'John' ],
]);
(new FastExcel($list))->export('file.xlsx');
核心功能详解
多种格式支持
FastExcel支持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');
高级功能
多工作表导出
创建SheetCollection来导出多个工作表:
use Rap2hpoutre\FastExcel\FastExcel;
use Rap2hpoutre\FastExcel\SheetCollection;
$sheets = new SheetCollection([
User::all(),
Project::all()
]);
(new FastExcel($sheets))->export('file.xlsx');
使用索引指定工作表名称:
$sheets = new SheetCollection([
'Users' => User::all(),
'Second sheet' => Project::all()
]);
多工作表导入
导入多个工作表:
$sheets = (new FastExcel)->importSheets('file.xlsx');
导入特定工作表:
$users = (new FastExcel)->sheet(3)->import('file.xlsx');
带工作表名称的导入:
$sheets = (new FastExcel)->withSheetsNames()->importSheets('file.xlsx');
大数据量处理
使用生成器避免内存限制:
function usersGenerator() {
foreach (User::cursor() as $user) {
yield $user;
}
}
// 即使有1000万行数据,导出也只需几MB内存
(new FastExcel(usersGenerator()))->export('test.xlsx');
样式设置
添加标题行和行样式:
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');
导入功能
基础导入
导入Excel文件并返回集合:
$collection = (new FastExcel)->import('file.xlsx');
CSV配置导入
配置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']
]);
});
辅助功能
门面使用
在config/app.php的aliases键下添加:
'FastExcel' => Rap2hpoutre\FastExcel\Facades\FastExcel::class,
使用门面:
$list = collect([
[ 'id' => 1, 'name' => 'Jane' ],
[ 'id' => 2, 'name' => 'John' ],
]);
FastExcel::data($list)->export('file.xlsx');
全局助手
FastExcel提供便捷的全局助手:
$collection = fastexcel()->import('file.xlsx');
fastexcel($collection)->export('file.xlsx');
性能优势
FastExcel在处理大量数据时具有显著优势。在测试中,导出10000行20列随机数据的XLSX文件时:
- 内存使用:FastExcel平均峰值内存使用为2.09MB,而Laravel Excel为123.56MB
- 执行时间:FastExcel为2.76秒,而Laravel Excel为11.56秒
使用建议
FastExcel适用于简单的Excel导入导出任务。对于需要复杂Excel操作的情况,建议使用功能更丰富的Laravel Excel库。FastExcel的优势在于其轻量级和高效的内存管理,特别适合处理大数据量的场景。
通过本教程,您应该已经掌握了FastExcel的核心功能和使用方法。现在就可以在您的Laravel项目中应用这些技巧,享受快速Excel处理带来的效率提升。
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考



