0

I'm using Laravel-Excel to create excel files with Laravel. I need to set page orientation to landscape, But I can't find the solution in official documentation. By the way I'm using Blade to create excel file and this is my code.

class ExampleExcel implements FromView, WithEvents {

    public function view(): View {
        return view('excel.main');
    }

    public function registerEvents(): array {
        return [
            AfterSheet::class => function(AfterSheet $event) {
                $event->sheet->getDelegate()->setRightToLeft(true);
            },
        ];
    }

}

Thanks in advance!

Logan
  • 3
  • 2
  • It uses phpSpreadsheet so you should get the spreadsheet object then set the landscape. https://stackoverflow.com/questions/54939915/how-to-set-page-orientationlandscape-or-portrait-in-phpspreadsheet – Anurat Chapanond Oct 15 '20 at 19:40
  • @AnuratChapanond - I think this solution is not working for version 3.1. There's no `PhpOffice\PhpSpreadsheet\Worksheet\PageSetup` class – Logan Oct 15 '20 at 19:56

2 Answers2

3

Consider adding BeforeSheet event like this.

return [
    BeforeSheet::class => function (BeforeSheet $event) {
        $event->sheet
            ->getPageSetup()
            ->setOrientation(\PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_LANDSCAPE);
    },
];
2

You can use beforeSheet event to get spreadsheet object then in the event you can set orientation.

public static function beforeSheet(BeforeSheet $event)
{
    $event->sheet->getActiveSheet()->getPageSetup()
        ->setOrientation(\PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_LANDSCAPE);
}

You can read more about events at https://docs.laravel-excel.com/3.0/exports/extending.html

Anurat Chapanond
  • 2,837
  • 3
  • 18
  • 31