How can I change color a character laravel excel maatwebsite?
Asked Answered
G

1

5

I get tutorial from here : https://laravel-excel.maatwebsite.nl/3.0/exports/extending.html

So I use version 3

My excel like this :

enter image description here

I want to change it to be like this :

enter image description here

So I want the character England change to red color and bold

I try like this :

namespace App\Exports;
...
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\BeforeExport;
use Maatwebsite\Excel\Events\AfterSheet;

class SummaryExport implements FromView, WithEvents
{
    ...
    public function registerEvents(): array
    {
        return [
            AfterSheet::class    => function(AfterSheet $event) {
                $event->sheet->styleCells(
                    'B1:D1',
                    [
                        'borders' => [
                            'outline' => [
                                'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
                                'color' => ['argb' => 'EB2B02'],
                            ],
                        ]
                    ]
                );
            },
        ];
    }
}

There exist error like this :

Method Maatwebsite\Excel\Sheet::styleCells does not exist.

How can I solve this error?

Gargantua answered 6/8, 2018 at 8:49 Comment(8)
did you create styleCells macro ?Kruter
@Kruter No. Where to create it? I did not find it in the documentationGargantua
Create a service provider. Put your macro code on it's boot method.Bracing
@Bracing Just try to answer this question. I did not see the process in the documentationGargantua
@SuccessMan try my answer.Bracing
@Mahbu Okay I will try itGargantua
@SuccessMan here is the document laravel-excel.maatwebsite.nl/3.0/exports/…Kruter
@Kruter Seems you can help me again. Look at this : #51733702Gargantua
B
7

You may register macro within a service provider's boot method. For example App\Providers\AppServiceProvider provider's will look like as: <?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use \Maatwebsite\Excel\Sheet;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
        Sheet::macro('styleCells', function (Sheet $sheet, string $cellRange, array $style) {
            $sheet->getDelegate()->getStyle($cellRange)->applyFromArray($style);
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

You should create different service provider to resister this kind of macro for isolating third party's concern.

For font color set font style :

namespace App\Exports;
...
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\BeforeExport;
use Maatwebsite\Excel\Events\AfterSheet;

class SummaryExport implements FromView, WithEvents
{
    ...
    public function registerEvents(): array
    {
        return [
            AfterSheet::class    => function(AfterSheet $event) {
                $event->sheet->styleCells(
                    'B1:D1',
                    [
                        //Set border Style
                        'borders' => [ 
                            'outline' => [
                                'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
                                'color' => ['argb' => 'EB2B02'],
                            ],
                           
                        ],

                        //Set font style
                        'font' => [
                            'name'      =>  'Calibri',
                            'size'      =>  15,
                            'bold'      =>  true,
                            'color' => ['argb' => 'EB2B02'],
                        ],

                        //Set background style
                        'fill' => [
                            'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
                            'startColor' => [
                                'rgb' => 'dff0d8',
                             ]           
                        ],

                    ]
                );
            },
        ];
    }
}
Bracing answered 6/8, 2018 at 9:43 Comment(5)
I had try it. Now, it does not error. But the character England not change to red color and boldGargantua
Seems you can help me again. Look at this : #51733702Gargantua
How to do this by specific rows, by condition?Attire
How to set background, pleaseAttire
For v3.1: https://mcmap.net/q/665083/-how-can-i-set-text-align-right-in-the-column-on-the-laravel-excel-maatwebsitePanzer

© 2022 - 2024 — McMap. All rights reserved.