0

I get reference from here : https://laravel-excel.maatwebsite.nl/3.0/getting-started/

I have been looking for how to set the text align right, but I did not find it in the documentation

My script export like this :

<?php
namespace App\Exports;
use Maatwebsite\Excel\Concerns\Exportable;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
class InvoiceExport implements FromView
{
    use Exportable;
    public function view(): View
    {
        $data = Invoice::get();
        return view('exports.item', [
            'data' => $data
        ]);
    }
}

How can I solve this problem?

Update

I find a solution, but it's not perfect

public function registerEvents(): array
{
    return [
        AfterSheet::class    => function(AfterSheet $event) {
            $event->sheet->styleCells(
                'C2:C1000',
                [
                    'alignment' => [
                        'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_RIGHT,
                    ],
                ]
            );
        },
    ];
}

It works. But my record is dynamic. It can be 1000 records. it can be 10000 records

In my script above, it just block from C2 to C1000. I want to set all records in column C

How can I do it?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
moses toh
  • 12,344
  • 71
  • 243
  • 443

4 Answers4

3

For Laravel Excel 3.1.12, we could use registerEvents and Macro :


use Maatwebsite\Excel\Sheet;

...

    public function registerEvents(): array
    {
        return [
            // array callable, refering to a static method.
            AfterSheet::class => [self::class, 'afterSheet'],
        ];
    }

    public static function afterSheet(AfterSheet $event)
    {
        Sheet::macro('styleCells', function (Sheet $sheet, string $cellRange, array $style) {
            $sheet->getDelegate()->getStyle($cellRange)->applyFromArray($style);
        });

        $event->sheet->styleCells('D:D', [
            'alignment' => [
                'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_RIGHT,
            ],
        ]);
    }

...
Dendi Handian
  • 348
  • 2
  • 12
2

Just use C:C which will select the entire column:

'C:C',
[
    'alignment' => [
        'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_RIGHT,
    ],
]
FurBurger
  • 71
  • 1
  • 3
0

The below code works for me

 $event->sheet->getStyle('A1:B48')->getAlignment()->setHorizontal('center');
Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39
Rahul S
  • 11
  • 2
0

1 - First You Add This On The Top:

AfterSheet

use Maatwebsite\Excel\Events\{
        BeforeExport,
        AfterSheet
};

2 - Second implements WithEvents With YourClass

WithEvents

class YourClass implements WithEvents {

3 - Third Add This On Body Of Class:

getAlignment

    public function registerEvents(): array
        {
            return [
                AfterSheet::class => function(AfterSheet $event) {
                  // multi cols
                  $event->sheet->getStyle('A:B')->getAlignment()->setHorizontal('center');
                  // single col
                  $event->sheet->getStyle('D')->getAlignment()->setHorizontal('center');
                },
            ];
        }