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 协议》,转载必须注明作者和本文链接
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!