Laravel excel 将复杂 headers 添加到导出
Laravel excel add complex headers to an export
我需要通过 Laravel Excel 导出带有复杂标题的 sheet。我需要一个主标题和另一个 sub-level 标题。
我正在尝试这样,
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadings;
class InvoicesExport implements FromQuery, WithHeadings
{
public function headings(): array
{
return [
'Account 1' =>[
"Account 1 id",
"Account 1 branch"
],
'Account 2' =>[
"Account 2 id",
"Account 2 branch"
],
];
}
}
但是得到 header 列,例如 [“账户 1 id”、“账户 1 分行”]
有没有办法存档这个任务?
终于,我成功了。添加它,因为它对其他人有用。
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithCustomStartCell;
use Maatwebsite\Excel\Events\AfterSheet;
use Maatwebsite\Excel\Concerns\WithEvents;
use \Maatwebsite\Excel\Sheet;
class InvoicesExport implements FromCollection, WithHeadings, WithCustomStartCell, WithEvents{
public function startCell(): string
{
return 'A2';
}
public function registerEvents(): array {
return [
AfterSheet::class => function(AfterSheet $event) {
/** @var Sheet $sheet */
$sheet = $event->sheet;
$sheet->mergeCells('A1:B1');
$sheet->setCellValue('A1', "Account 1");
$sheet->mergeCells('C1:D1');
$sheet->setCellValue('C1', "Account 2");
$styleArray = [
'alignment' => [
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
],
];
$cellRange = 'A1:D1'; // All headers
$event->sheet->getDelegate()->getStyle($cellRange)->applyFromArray($styleArray);
},
];
}
public function headings(): array
{
return [
"Account 1 id",
"Account 1 branch"
"Account 2 id",
"Account 2 branch"
];
}
}
这里我添加了 startCell() 从第二行开始。 registerEvents() 将第一行单元格与内容合并并居中对齐。
我需要通过 Laravel Excel 导出带有复杂标题的 sheet。我需要一个主标题和另一个 sub-level 标题。
我正在尝试这样,
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadings;
class InvoicesExport implements FromQuery, WithHeadings
{
public function headings(): array
{
return [
'Account 1' =>[
"Account 1 id",
"Account 1 branch"
],
'Account 2' =>[
"Account 2 id",
"Account 2 branch"
],
];
}
}
但是得到 header 列,例如 [“账户 1 id”、“账户 1 分行”]
有没有办法存档这个任务?
终于,我成功了。添加它,因为它对其他人有用。
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithCustomStartCell;
use Maatwebsite\Excel\Events\AfterSheet;
use Maatwebsite\Excel\Concerns\WithEvents;
use \Maatwebsite\Excel\Sheet;
class InvoicesExport implements FromCollection, WithHeadings, WithCustomStartCell, WithEvents{
public function startCell(): string
{
return 'A2';
}
public function registerEvents(): array {
return [
AfterSheet::class => function(AfterSheet $event) {
/** @var Sheet $sheet */
$sheet = $event->sheet;
$sheet->mergeCells('A1:B1');
$sheet->setCellValue('A1', "Account 1");
$sheet->mergeCells('C1:D1');
$sheet->setCellValue('C1', "Account 2");
$styleArray = [
'alignment' => [
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
],
];
$cellRange = 'A1:D1'; // All headers
$event->sheet->getDelegate()->getStyle($cellRange)->applyFromArray($styleArray);
},
];
}
public function headings(): array
{
return [
"Account 1 id",
"Account 1 branch"
"Account 2 id",
"Account 2 branch"
];
}
}
这里我添加了 startCell() 从第二行开始。 registerEvents() 将第一行单元格与内容合并并居中对齐。