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

noblemfd's avatar

How to add Title to Excel Sheet using Maatwebsites

I am trying to export my report to excel in Laravel-5.8 project using Maatwebsites:

In Laravel-5.8, I am trying to export to Excel using Maatwebsite:

<?php

namespace App\Exports;

use App\User;
use Carbon\Carbon;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\BeforeExport;
use Maatwebsite\Excel\Events\BeforeWriting;
use Maatwebsite\Excel\Events\BeforeSheet;
use Maatwebsite\Excel\Events\AfterSheet;

use Maatwebsite\Excel\Concerns\FromView;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithMapping;
use \Maatwebsite\Excel\Sheet;
use Maatwebsite\Excel\Concerns\RegistersEventListeners;
use Illuminate\Support\Facades\DB;

use Auth;


class EmployeesGoalPublishedExport implements FromCollection, ShouldAutoSize, WithHeadings
{


    private $headings = [
        'Staff ID', 
        'Name',
        'Gender',
        'Official Email',
        'Department',
        'HOD',
        'HRBP',
        'Line Manager',
        'Goal Status'
    ];




    public function collection()
    {
        $userCompany = Auth::user()->company_id;
        $userEmployee = Auth::user()->employee_id;
        $userId = Auth::user()->id;
        $employeecode = Auth::user()->employee_code;

        $identities = DB::table('appraisal_identity')->select('id')->where('company_id', $userCompany)->where('is_current', 1)->pluck('id');
        $publishedgoals = AppraisalGoal::select('employee_code')->where('is_published', 1)->where('appraisal_identity_id', $identities)->where('company_id', $userCompany)->groupBy('employee_code')->get();  


        $published_goals = DB::table('hr_employees AS e')
                    ->join('hr_employees AS em','em.employee_code','=','e.line_manager_id')
                    ->join('hr_departments AS d','e.department_id','=','d.id')
                    ->join('hr_employees AS eh','eh.employee_code','=','d.dept_head')
                    ->join('hr_employees AS eb','eb.employee_code','=','d.hr_business_partner_id')
                    ->join('appraisal_goals AS ag','ag.company_id','=','e.company_id')
                     ->select(
                        'e.employee_code as staff_id',
                        DB::raw('CONCAT(e.first_name, " ", e.last_name) AS full_name'),
                        DB::raw('IF(e.gender_code = 0, "Female", "Male") AS gender'),      
                        'e.email as official_email',
                        'd.dept_name',

                        DB::raw('CONCAT(eh.first_name, " ", eh.last_name) AS hod_name'),
                        DB::raw('CONCAT(eb.first_name, " ", eb.last_name) AS hrbp_name'),
                        DB::raw('CONCAT(em.first_name, " ", em.last_name) AS line_manager'),
                        DB::raw('(CASE WHEN ag.is_approved = 3 THEN "Approved" WHEN ag.is_approved = 2 THEN "Not Approved" WHEN ag.is_approved = 1 THEN "Awaiting Approval" ELSE "Draft" END) AS goal_status')
                 )
                    ->whereIn('e.employee_code', $publishedgoals)
                    ->where('e.company_id', $userCompany)
                    ->where('e.hr_status', 0) 
                ->distinct()
                ->get();

            $published_goals = $published_goals->unique('staff_id');
            $published_goals = array_slice($published_goals->values()->all(), 0, 5, true);          

                 return $published_goals;
    } 

    public function headings() : array
    {

                        return $this->headings;	//english
    }    

    public function registerEvents(): array
    {
        return [
            AfterSheet::class    => function(AfterSheet $event) {
                $cellRange = 'A1:I1'; // All headers
                $event->sheet->getDelegate()->getStyle($cellRange)->getFont()->setSize(14);
            },

        ];
    }     
}

How to I add General Title on the sheet on top of the headings, and also center the Title.

How do I make the headings and the Title bold.

Thanks

0 likes
6 replies
MichalOravec's avatar

Something like this propably,

public function registerEvents()
{
   return [
       AfterSheet::class => function(AfterSheet $event) {
           $event->sheet->setCellValue('A1:I1', 'STUDENT PERFORMANCE');

           // $cells->setAlignment('center')

           $event->sheet->getDelegate()->getStyle($cellRange)->getFont()->setSize(14);
       },
   ];
}

WIth setAlignment('center') you set align to center but you have to find your cells

MichalOravec's avatar

Look into documentation or search on internet. Maybe here somebody else will know the solution.

Please or to participate in this conversation.