lunia-consultores/laravel-excel

该软件包提供了一种将 Eloquent 集合导出为 Excel 文件以及将 Excel 文件导入为 Eloquent 集合的方法。

v1.0 2023-08-31 08:25 UTC

This package is not auto-updated.

Last update: 2024-09-27 12:06:50 UTC


README

从 legacy Maatwebsite/Laravel-Excel 分支出来以支持 Laravel 10

安装

使用以下命令通过 composer 安装此包:

composer require lunia-consultores/laravel-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 文件第一张工作表的每一行都成为一个数组,最终结果被包装在一个集合(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');