maatwebsite/excel使用:导出——自定义列
自定义列
格式化列
你可以轻松地格式化整个列,方法是使用 WithColumnFormatting
。如果你需要更自定义的功能,建议使用 AfterSheet
事件直接与底层的 Worksheet
类进行交互。
namespace App\Exports;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithMapping;
class InvoicesExport implements WithColumnFormatting, WithMapping
{
public function map($invoice): array
{
return [
$invoice->invoice_number,
Date::dateTimeToExcel($invoice->created_at),
$invoice->total
];
}
public function columnFormats(): array
{
return [
'B' => NumberFormat::FORMAT_DATE_DDMMYYYY,
'C' => NumberFormat::FORMAT_CURRENCY_EUR_INTEGER,
];
}
}
日期
在处理日期时,建议在映射中使用 \PhpOffice\PhpSpreadsheet\Shared\Date::dateTimeToExcel()
以确保日期的正确解析。
值绑定器
默认情况下,Laravel Excel 使用 PhpSpreadsheet 的默认值绑定器来智能地格式化读取单元格的值。你可以通过实现 WithCustomValueBinder
和 bindValue
方法来覆盖此行为。你的导出类也可以扩展 DefaultValueBinder
以返回默认行为。
namespace App\Exports;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use Maatwebsite\Excel\Concerns\ToModel;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;
class UsersExport extends DefaultValueBinder implements WithCustomValueBinder
{
public function bindValue(Cell $cell, $value)
{
if (is_numeric($value)) {
$cell->setValueExplicit($value, DataType::TYPE_NUMERIC);
return true;
}
// 否则返回默认行为
return parent::bindValue($cell, $value);
}
}
可用的数据类型
PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING
PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_FORMULA
PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_NUMERIC
PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_BOOL
PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_NULL
PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_INLINE
PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_ERROR
禁用智能格式化
如果你想禁用值的智能格式化,可以通过扩展你的导出类并使用 \PhpOffice\PhpSpreadsheet\Cell\StringValueBinder
。在这种情况下,所有值都将作为字符串传递。
namespace App\Exports;
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
class UsersExport extends \PhpOffice\PhpSpreadsheet\Cell\StringValueBinder implements WithCustomValueBinder
{
}
默认值绑定器
如果你想为所有导出使用一个值绑定器,可以在配置中设置默认值绑定器。
在 config/excel.php
中:
'value_binder' => [
'default' => Maatwebsite\Excel\DefaultValueBinder::class,
],
自动调整大小
如果你想让 Laravel Excel 执行自动宽度计算,需要实现 ShouldAutoSize
接口,如下所示。
namespace App\Exports;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
class InvoicesExport implements ShouldAutoSize
{
...
}
列宽
在某些情况下,你可能希望对实际列宽有更多控制,而不是依赖于自动调整大小。你可以使用 WithColumnWidths
来实现这一点。它接受一个列数组(字母表示:A, B, C)和一个数值宽度。
namespace App\Exports;
use Maatwebsite\Excel\Concerns\WithColumnWidths;
class InvoicesExport implements WithColumnWidths
{
public function columnWidths(): array
{
return [
'A' => 55,
'B' => 45,
];
}
}
可以与 ShouldAutoSize
一起使用。只有具有显式宽度的列不会被自动调整大小。
样式
WithStyles
(从 v3.1.21
开始可用)允许对列、单元格和行进行样式设置。当你想将标题行设置为粗体时,这可能会很有用。
namespace App\Exports;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class InvoicesExport implements WithStyles
{
public function styles(Worksheet $sheet)
{
return [
// 将第一行设置为粗体文本。
1 => ['font' => ['bold' => true]],
// 通过坐标设置特定单元格的样式。
'B2' => ['font' => ['italic' => true]],
// 设置整个列的样式。
'C' => ['font' => ['size' => 16]],
];
}
}
有关样式数组的内容,请参阅 PhpSpreadsheet 文档
如果你更喜欢使用流畅的语法来设置单元格样式,可以这样做:
namespace App\Exports;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class InvoicesExport implements WithStyles
{
public function styles(Worksheet $sheet)
{
$sheet->getStyle('B2')->getFont()->setBold(true);
}
}
默认样式
WithDefaultStyles
(从 v3.1.40
开始可用)允许对整个工作簿进行样式设置。
namespace App\Exports;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\Style;
use PhpOffice\PhpSpreadsheet\Style\Color;
use Maatwebsite\Excel\Concerns\WithDefaultStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class InvoicesExport implements WithDefaultStyles
{
public function defaultStyles(Style $defaultStyle)
{
// 配置默认样式
return $defaultStyle->getFill()->setFillType(Fill::FILL_SOLID);
// 或者返回样式数组
return [
'fill' => [
'fillType' => Fill::FILL_SOLID,
'startColor' => ['argb' => Color::RED],
],
];
}
}
工作簿背景颜色
WithBackgroundColor
(从 v3.1.40
开始可用)支持配置整个工作簿的背景颜色。
namespace App\Exports;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\Style;
use PhpOffice\PhpSpreadsheet\Style\Color;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Maatwebsite\Excel\Concerns\WithBackgroundColor;
class InvoicesExport implements WithBackgroundColor
{
public function backgroundColor()
{
// 返回 RGB 颜色代码。
return '000000';
// 返回一个 Color 实例。填充类型将自动设置为 "solid"
return new Color(Color::COLOR_BLUE);
// 或者返回样式数组
return [
'fillType' => Fill::FILL_GRADIENT_LINEAR,
'startColor' => ['argb' => Color::COLOR_RED],
];
}
}
完整的样式映射
完整的样式映射可以从 PhpSpreadsheet Styles 类中获取,如下所示:
/**
* 从数组应用样式。
*/
$spreadsheet->getActiveSheet()->getStyle('B2')->applyFromArray(
[
'font' => [
'name' => 'Arial',
'bold' => true,
'italic' => false,
'underline' => Font::UNDERLINE_DOUBLE,
'strikethrough' => false,
'color' => [
'rgb' => '808080'
]
],
'borders' => [
'bottom' => [
'borderStyle' => Border::BORDER_DASHDOT,
'color' => [
'rgb' => '808080'
]
],
'top' => [
'borderStyle' => Border::BORDER_DASHDOT,
'color' => [
'rgb' => '808080'
]
]
],
'alignment' => [
'horizontal' => Alignment::HORIZONTAL_CENTER,
'vertical' => Alignment::VERTICAL_CENTER,
'wrapText' => true,
],
'quotePrefix' => true
]
);
本作品采用《CC 协议》,转载必须注明作者和本文链接