Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

youssefboudaya's avatar

How to properly style excel file with laravel?

I'm trying to export data from database into excel file.this is the code i'm using to display my data into the newly created excel file:

$name = 'fiche signaletique '.date('d-m-Y H-i');  

        Excel::create($name, function($excel) use($cards, $count) {

            $excel->sheet('Sheetname', function($sheet) use($cards, $count) {

                $i = 2;
                $rows = $count;
                $rows++;

                $sheet->setHeight(1, 25);
                $sheet->setAutoSize(true);
                $sheet->setBorder('A1:AI'.$rows, 'thin');

                $sheet->row(1, array(
                    
                   'Client (Société)',
                    'Numéro Fiche',
                    'Forme juridique',
                    'Capital Social',
                    'Type',
                    'Gouvernorat',
                    'Adresse',
                    'Code postal',
                    'Numéro Registre de Commerce',
                    'Tribunal de première Instance',
                    'Adresse mail',
                    'Activité',
                    'Régime export',
                    'Premier responsable',
                    'Qualité',
                    'GSM premier responsable',
                    'Téléphone 1',
                    'Téléphone 2',
                    'Fax',
                    'Identifiant fiscal',
                    'Code TVA',
                    'Code catégorie',
                    'Etablissements secondaire',
                    'Numéro Agrément API',
                    'Nature du projet',
                    'Date agrément',
                    'Bureau de contrôle des impôts',
                    'Matricule CNSS',
                    'Matricule CNSS Indépendant',
                    'Taux CNSS employeur',
                    'Taux accident de travail',
                    'Bureau CNSS',
                    'Régime horaire',
                    'Notre mission',
                    'Observation',
                    'Date de création'

                ))->cells('A1:AI1', function($cells) {

                    $cells->setBackground('#1E86CF');
                    $cells->setFont(array(
                        'family'     => 'Calibri',
                        'size'       => '12',
                        'bold'       =>  true
                    ));
                    $cells->setFontColor('#ffffff');

                });

                foreach ($cards as $key => $card) {
                    $sheet->row($i, array(

                        $card->clientsociete['company'], 
                        $card->card_code, 
                        $card->legal_form,
                        $card->share_capital,
                        $card->group,
                        $card->clientgov['title'],
                        $card->adress,
                        $card->zip_code,
                        $card->trade_register,
                        $card->clientcourt['title'],
                        $card->email,
                        $card->activity,
                        $card->export_regime,
                        $card->responsible,
                        $card->quality,
                        $card->phone_1,
                        $card->phone_2,
                        $card->fax,
                        $card->gsm_responsible,
                        $card->tax_identification,
                        $card->tva_code,
                        $card->category_code,
                        $card->secondary_establishments,
                        $card->api_registration,
                        $card->nature_project,
                        $card->approval_date,
                        $card->clientcontrol['title'],
                        $card->cnss_number,
                        $card->cnss_number_independent,
                        $card->cnss_rate_employer,
                        $card->accident_rate_work,
                        $card->clientcnss['title'],
                        $card->hourly_plan,
                        $card->our_mission,
                        $card->observation,
                        $card->created_at

                    ))->cells('A'.$i, function($cells) {

                        $cells->setBackground('#D9D9D9');

                    });

                    $i++;
                }    

            });

        })->download('xls');

The problem that i have is that the last column doesn't have any style(no font or background color).

0 likes
0 replies

Please or to participate in this conversation.