以全宽度的页眉和页脚导出

时间:2019-10-14 02:37:32

标签: php laravel phpspreadsheet laravel-excel

我正在使用Laravel Excel将文件导出为CSV,HTML和PDF(带有TCPDF)

我正在尝试在所有页面上使用页眉和页脚导出它,但在导出的文件上看不到它们。

此外,归档结果非常缩小,没有占用页面的整个宽度。 如何使它占据足够的空间宽度。

那是我的代码:

public function registerEvents(): array
    {
        return [
            BeforeExport::class => function (BeforeExport $event) {
                $event->writer->getProperties()
                    ->setTitle("Test Document")
                    ->setSubject("Test Document");
            },

            AfterSheet::class => function (AfterSheet $event) {
                $event->sheet->getDelegate()->getPageSetup()->setOrientation(PageSetup::ORIENTATION_DEFAULT);
                $event->sheet->getDelegate()->getPageSetup()->setPaperSize(PageSetup::PAPERSIZE_A4);
                $event->sheet->getDelegate()->getPageSetup()->setFitToWidth(1);
                $event->sheet->getDelegate()->getPageSetup()->setFitToHeight(0);
                $event->sheet->getDelegate()->getPageSetup()->setScale(100);

                $event->sheet->getDelegate()->getPageSetup()->setHorizontalCentered(true);
                $event->sheet->getDelegate()->getHeaderFooter()->setOddHeader('My Header');
                $event->sheet->getDelegate()->getHeaderFooter()->setOddFooter('My Footer');

                // last column as letter value (e.g., D)
                $last_column = (count($this->results)) ? Coordinate::stringFromColumnIndex(count($this->results[0])) : 'W';
                // calculate last row (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);
            },
        ];
    }

当前结果如此处所示 Shrinked on width

如何使其占据页面的整个宽度以使其看起来不错。

0 个答案:

没有答案