cyber-duck / laravel-excel
此包提供了一种将 Eloquent 集合导出为 Excel 文件以及将 Excel 文件导入为 Eloquent 集合的方法。
Requires
- php: ^7.3
- box/spout: ^3.1
- illuminate/database: ^6.0.0|^7.0.0|^8.0.0
- illuminate/support: ^6.0.0|^7.0.0|^8.0.0
Requires (Dev)
- laravel/laravel: ^6.0.0|^7.0.0|^8.0.0
- phpspec/phpspec: ^7.0.0
- phpunit/phpunit: ^9.5.0
README
使用 Laravel (5.* 和 4.*) 中的 Eloquent 集合和查询构建器导出和导入 Excel、CSV 和 OpenOffice 样式表。
它基于 box/spout。
作者: Simone Todaro
贡献者: Clément Blanco
由 Cyber-Duck Ltd 制作
安装
使用 composer 下载包
composer require cyber-duck/laravel-excel
Laravel 4.x
在 config/app.php
中注册服务提供者,通过添加以下行到 providers 数组。
'providers' => [ Cyberduck\LaravelExcel\ExcelLegacyServiceProvider::class, ],
Laravel < 5.5
在 config/app.php
中注册服务提供者,通过添加以下行到 providers 数组。
'providers' => [ Cyberduck\LaravelExcel\ExcelServiceProvider::class, ],
Laravel > 5.5
无需注册,因为它在 Laravel 5.5 中使用了包自动发现功能。
导出 Excel
生成并下载 Excel 文件
添加
use Exporter;
到您的控制器。
在您的控制器函数中,从 Eloquent 集合创建一个新的 Excel 文件。
$excel = Exporter::make('Excel');
$excel->load($yourCollection);
return $excel->stream($yourFileName);
导出类是流畅的,因此您也可以这样写
return Exporter::make('Excel')->load($yourCollection)->stream($yourFileName);
导出类也支持查询构建器对象
$query = DB:table('table')->select('col1','col2');
$excel = Exporter::make('Excel');
$excel->loadQuery($query);
return $excel->stream($yourFileName);
如果您处理大表,可以设置块大小以最小化内存使用
$query = DB:table('table')->select('col1','col2');
$excel = Exporter::make('Excel');
$excel->loadQuery($query);
$excel->setChunk(1000);
return $excel->stream($yourFileName);
生成并保存 Excel 文件
要保存 Excel 文件到服务器,请使用 save 方法。
return $excel->save($yourFileNameWithPath);
高级使用
默认情况下,集合的每个元素都成为一行,模型的每个未受保护的字段都成为一个单元格。
不打印标题行。
要更改此行为,创建一个扩展 Cyberduck\LaravelExcel\Contract\SerialiserInterface 的类,实现方法 getHeaderRow() 和 getData(Model $data),并使用 setSerialiser() 将此类设置在 Excel 对象上。
$serialiser = new CustomSerialiser();
$excel = Exporter::make('Excel');
$excel->load($collection);
$excel->setSerialiser($serialiser);
return $excel->stream($yourFileName);
getHeaderRow() 必须返回一个字符串数组,其中每个元素都是第一行的单元格。要打印标题行,只需返回一个空数组 []。
getData(Model $data) 必须返回一个字符串数组,每个元素都是单元格。
示例
namespace App\Serialisers;
use Illuminate\Database\Eloquent\Model;
use Cyberduck\LaravelExcel\Contract\SerialiserInterface;
class ExampleSerialiser implements SerialiserInterface
{
public function getData($data)
{
$row = [];
$row[] = $data->field1;
$row[] = $data->relationship->field2;
return $row;
}
public function getHeaderRow()
{
return [
'Field 1',
'Field 2 (from a relationship)'
];
}
}
然后设置序列化器,在保存集合文件之前。
$collection = Exporter::make('Excel')->load($yourCollection)->setSerialiser(new ExampleSerialiser)->stream($yourFileName);
导入 Excel
添加
use Importer;
到您的控制器。
在您的控制器函数中,导入一个 Excel 文件。
$excel = Importer::make('Excel');
$excel->load($filepath);
$collection = $excel->getCollection();
//dd($collection)
导入类是流畅的,因此您也可以这样写
return Importer::make('Excel')->load($filepath)->getCollection();
高级使用
默认情况下,Excel 文件第一张表中的每一行都变成一个数组,最终结果是包裹在 Collection (Illuminate\Support\Collection) 中。
要导入不同的表,使用 setSheet($sheet)
$excel = Importer::make('Excel');
$excel->load($filepath);
$excel->setSheet($sheetNumber);
$collection = $excel->getCollection();
//dd($collection)
要导入每个 Eloquent 模型的行,创建一个扩展 Cyberduck\LaravelExcel\Contract\ParserInterface 的类,并实现方法 transform($row)。
示例
namespace App\Parsers;
use App\Models\YourModel;
use Cyberduck\LaravelExcel\Contract\ParserInterface;
class ExampleParser implements ParserInterface
{
public function transform($row, $header)
{
$model = new YourModel();
$model->field1 = $row[0];
$model->field2 = $row[1];
// We can manunipulate the data before returning the object
$model->field3 = new \Carbon($row[2]);
return $model;
}
}
然后设置解析器,在创建集合之前。
$collection = Importer::make('Excel')->load($filepath)->setParser(new ExampleParser)->getCollection();
不同格式
此包支持 ODS 和 CSV 文件。
ODS
$exporter = Exporter::make('OpenOffice');
$importer = Importer::make('OpenOffice');
CSV
$exporter = Exporter::make('Csv');
$importer = Importer::make('Csv');