使用 Laravel Excel 包添加自定义页脚和页眉

Adding Custom Footer and Header with Laravel Excel Package

我正在使用 Laravel Excel 版本 3.1 生成 CSV/XLS 等。 我必须自定义我的 csv 或 xls 以具有跨列的自定义页脚和页眉。

它应该看起来像所附的屏幕截图。

此外,我想将导出的文件存储到存储位置,但无法正常工作。

而我做的代码是:

class ReportExport implements FromArray, WithHeadings
{
    protected $results;

    public function __construct(array $results, array $headings, array $fileAttributes)
    {
        $this->results = $results;
        $this->headings = $headings;
        $this->file_attributes = $fileAttributes;
    }

    /**
     * @return array
     */
    public function array(): array
    {
        return $this->results;
    }

    /**
     * @return array
     */
    public function headings(): array
    {
        return $this->headings;
    }

    public function registerEvents(): array
    {
        return [
            // Handle by a closure.
            BeforeExport::class => function(BeforeExport $event) {
                $event->writer->getProperties()->setTitle('Patrick');
            },
        ];
    }

}

如下调用 i:

Excel::store(["1","2"],"xyz.xlsx");

如何将这些额外的行添加到导出的结果中。

Laravel Excel - 扩展
https://docs.laravel-excel.com/3.1/exports/extending.html

PHP电子表格
https://phpspreadsheet.readthedocs.io/en/latest/

还有一些空间可以清理,但这应该给你一些基础知识。

public function registerEvents(): array
{
    return [
        // Handle by a closure.
        AfterSheet::class => function(AfterSheet $event) {

            // last column as letter value (e.g., D)
            $last_column = Coordinate::stringFromColumnIndex(count($this->results[0]));

            // calculate last row + 1 (total results + header rows + column headings row + new row)
            $last_row = count($this->results) + 2 + 1 + 1;

            // set up a style array for cell formatting
            $style_text_center = [
                'alignment' => [
                    'horizontal' => Alignment::HORIZONTAL_CENTER
                ]
            ];

            // at row 1, insert 2 rows
            $event->sheet->insertNewRowBefore(1, 2);

            // merge cells for full-width
            $event->sheet->mergeCells(sprintf('A1:%s1',$last_column));
            $event->sheet->mergeCells(sprintf('A2:%s2',$last_column));
            $event->sheet->mergeCells(sprintf('A%d:%s%d',$last_row,$last_column,$last_row));

            // assign cell values
            $event->sheet->setCellValue('A1','Top Triggers Report');
            $event->sheet->setCellValue('A2','SECURITY CLASSIFICATION - UNCLASSIFIED [Generator: Admin]');
            $event->sheet->setCellValue(sprintf('A%d',$last_row),'SECURITY CLASSIFICATION - UNCLASSIFIED [Generated: ...]');

            // assign cell styles
            $event->sheet->getStyle('A1:A2')->applyFromArray($style_text_center);
            $event->sheet->getStyle(sprintf('A%d',$last_row))->applyFromArray($style_text_center);
        },
    ];
}

编辑:基本页面格式
这里有一些额外的东西可以帮助输出格式。这些可以附加到现有的 AfterSheet 事件。您可能想打开另一个问题来了解 PDF 操作的细节。

// set columns to autosize
for ($i = 1; $i <= count($this->results[0]); $i++) {
    $column = Coordinate::stringFromColumnIndex($i);
    $event->sheet->getColumnDimension($column)->setAutoSize(true);
}

// page formatting (orientation and size)
$event->sheet->getPageSetup()->setOrientation(\PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_LANDSCAPE);
$event->sheet->getPageSetup()->setPaperSize(\PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::PAPERSIZE_LETTER);