maatwebsite/excel使用:导出——设置
设置
属性
默认情况下,工作表属性在 config/excel.php
中配置。你可以设置默认的标题、描述、创建者等信息。
如果你想在每次导出时覆盖这些属性,可以使用 WithProperties
关注点。
namespace App\Exports;
use Maatwebsite\Excel\Concerns\WithProperties;
class InvoicesExport implements WithProperties
{
public function properties(): array
{
return [
'creator' => 'Patrick Brouwers',
'lastModifiedBy' => 'Patrick Brouwers',
'title' => 'Invoices Export',
'description' => 'Latest Invoices',
'subject' => 'Invoices',
'keywords' => 'invoices,export,spreadsheet',
'category' => 'Invoices',
'manager' => 'Patrick Brouwers',
'company' => 'Maatwebsite',
];
}
}
不需要返回所有属性,你可以省略不想覆盖的键。
namespace App\Exports;
use Maatwebsite\Excel\Concerns\WithProperties;
class InvoicesExport implements WithProperties
{
public function properties(): array
{
return [
'creator' => 'Patrick Brouwers',
];
}
}
自定义 CSV 设置
默认情况下,Laravel Excel 使用 config/excel.php
中的默认设置。你可以通过添加 WithCustomCsvSettings
接口来更改这些设置。
namespace App\Exports;
use Maatwebsite\Excel\Concerns\WithCustomCsvSettings;
class InvoicesExport implements WithCustomCsvSettings
{
public function getCsvSettings(): array
{
return [
'delimiter' => ';',
'use_bom' => false,
'output_encoding' => 'ISO-8859-1',
];
}
}
可用的 CSV 设置
delimiter
enclosure
line_ending
use_bom
include_separator_line
excel_compatibility
output_encoding
单元格缓存
默认情况下,PhpSpreadsheet 将所有单元格值保存在内存中。但在处理大文件时,这可能会导致内存问题。为了缓解这一问题,你可以配置一个单元格缓存驱动程序。
当使用 illuminate
驱动程序时,它会将每个值存储在缓存存储中。这可能会减慢处理速度,因为它需要存储每个值。然而,它会占用更少的内存。它会自动使用你的默认缓存存储。如果你希望将单元格缓存存储在一个单独的存储中,可以在这里配置存储名称。你可以使用缓存配置中定义的任何存储。如果留空,则使用默认存储。
你可以使用 batch
存储,当达到内存限制时才持久化到存储。你可以在配置中调整内存限制。
本作品采用《CC 协议》,转载必须注明作者和本文链接