gpt4 book ai didi

laravel-5 - Laravel excel 向导出添加复杂的标题

转载 作者:行者123 更新时间:2023-12-05 00:53:10 24 4
gpt4 key购买 nike

我需要通过 Laravel Excel 导出带有复杂标题的工作表.我需要一个主标题和另一个子标题。

enter image description here

我正在尝试这样,

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"
],
];
}
}

但是得到标题列,例如 ["Account 1 id", "Account 1 branch"]

有没有办法存档这个任务?

最佳答案

最后,我成功了。添加它,因为它对其他人有用。

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-5 - Laravel excel 向导出添加复杂的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68176458/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com