vim*_*uth 6 laravel-5 laravel-excel maatwebsite-excel
我需要通过Laravel Excel导出具有复杂标题的工作表。我需要一个主标题和另一个子标题。
我正在这样尝试,
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"
],
];
}
}
Run Code Online (Sandbox Code Playgroud)
但获取标题列,如 [“帐户 1 id”、“帐户 1 分支”]
有没有办法存档这个任务?
vim*_*uth 14
最后,我成功了。添加它,因为它对其他人有用。
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", \\here comma added
"Account 2 id",
"Account 2 branch"
];
}
}
Run Code Online (Sandbox Code Playgroud)
这里我添加了startCell()以从第二行开始。registerEvents()将第一行单元格与内容合并并居中对齐。
| 归档时间: |
|
| 查看次数: |
6862 次 |
| 最近记录: |