How can I set text align right in the column on the laravel Excel maatwebsite?
Asked Answered
W

4

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?

Winwaloe answered 7/8, 2018 at 18:23 Comment(12)
Maybe this answers your question, see: github.com/Maatwebsite/Laravel-Excel/issues/1597Simulator
@Oluwatobi Samuel Omisakin I using laravel excel maatwebsite version 3. No version 2Winwaloe
Unfortunately I am not sure 3.0 has that feature but I may be wrong. You can also check some familiar issues on Github: github.com/Maatwebsite/Laravel-Excel/…Simulator
@OluwatobiSamuelOmisakin I have seen that, but I did not find a solutionWinwaloe
In your exports.item blade file, just use <td style="text-align: right;">...</td> (or <td align="right"...) where you want right-aligned Excel cells. This is for version 2.1, but I'm sure the logic still applies: laravel-excel.maatwebsite.nl/2.1/blade/…Arch
@Tim Lewis I had try it. But it does not work in the version 3Winwaloe
I had update my question. Please look at thatWinwaloe
@SuccessMan try this github.com/Maatwebsite/Laravel-Excel/issues/79Finer
@RaheelAslam It works. But as I said in my question. My record is dinamyc. It can be 1000 records. it can be 10000 records. So I want to block all record in the column CWinwaloe
try to select entire column by 'C:C'.Jacalynjacamar
@Jacalynjacamar It does not workWinwaloe
I'm using Laravel Excel 3.1.32, and $event->sheet->styleCells() method isn't available...Narva
N
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,
            ],
        ]);
    }

...
Narva answered 2/9, 2021 at 7:29 Comment(1)
Works for me :). Just add use Maatwebsite\Excel\Sheet;Rech
A
2

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

'C:C',
[
    'alignment' => [
        'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_RIGHT,
    ],
]
Attaboy answered 2/5, 2020 at 23:58 Comment(0)
D
1

The below code works for me

 $event->sheet->getStyle('A1:B48')->getAlignment()->setHorizontal('center');
Duong answered 15/6, 2021 at 10:23 Comment(0)
S
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');
                },
            ];
        }
Sprinkle answered 12/7, 2023 at 7:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.