Maatwebsite exported excel sheet Row Height Issue

309 views Asked by At

I'm generating an excel spreadsheet using the maatwebsite excel function.

The issue I'm having is that I'm setting a default row height of 45. If you look at the image I attached enter image description here The row height is fine up to row 23, then on row 24 until end of data its back to a smaller size then after the end of data it looks like its back to 45.

Does anybody have any ideas on how I can fix this?

Here is my export file.

    <?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
use Maatwebsite\Excel\Concerns\WithColumnWidths;


class InquiryReportsExport implements FromCollection, WithHeadings, WithEvents, WithColumnWidths
{
    /**
     * @var Collection
     * @access protected
     */
    protected $inquiries;

    public function __construct($inquiries)
    {
        $this->inquiries = $inquiries;
    }

    /**
     * @return \Illuminate\Support\Collection
     */
    public function collection()
    {
        foreach ($this->inquiries as $inquiry) {
            $issue = [];

            foreach($inquiry->type_of_issues as $inquiry_issue) {
                array_push($issue, $inquiry_issue->type_of_issue_name);
            }
            $issuesValue = join(",",$issue);

            $rows[] = [
                $inquiry->sh_inquiry_date->format('m/d/Y'),
                'Inquiry',
                $inquiry->stakeholder->full_name,
                $inquiry->stakeholder->business_name,
                $inquiry->project_number,
                $inquiry->inquiry_method ? $inquiry->inquiry_method->type_of_contact_name : 'N/A',
                $issuesValue,
                'Opened'
            ];
            $rows[] = ['Concerns:',$inquiry->sh_inquiry_qc];
            $rows[] = ['Response/Actions:',$inquiry->sh_inquiry_sugg];

            // Add row if no followups
                    if ($inquiry->sh_inquiry_fudate == null && count($inquiry->followups) == 0) {
                        $rows[] = [
                            $inquiry->sh_inquiry_date->format('m/d/Y'),
                            'This Inquiry required no followups!',
                            ' ',
                            ' ',
                            ' ',
                            ' ',
                            ' ',
                            'Closed'
                        ];
                    }

            if($inquiry->followups) {

                foreach($inquiry->followups as $followup) {
                    if ($inquiry->sh_inquiry_fudate == null)
                        $followup_status = 'Closed';
                    else
                    $followup_status = ' ';

                    $rows[] = [
                        $followup->followup_date->format('m/d/Y'),
                        'Followup',
                        ' ',
                        ' ',
                        ' ',
                        $followup->type_of_contact ? $inquiry->inquiry_method->type_of_contact_name : 'N/A',
                        ' ',
                        $followup_status
                    ];
                    $rows[] = ['Response/Actions:',$followup->followup_action];

                }
            }
        }
        return collect($rows);
    }


    /**
     * @return array
     */
    public function headings(): array
    {
        return [
            'Date',
            'Type',
            'Name',
            'Business Name',
            'Project #',
            'Method',
            'Issue',
            'Status',
        ];
    }

          public function registerEvents(): array
    {
        return [
            AfterSheet::class=> function(AfterSheet $event) {
                    $event->sheet->getDelegate()->getStyle('A1:H1')
                        ->getFill()
                        ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
                        ->getStartColor()
                        ->setARGB('CCCCCC');

                    // Set blue background color for rows that contain the word inquiry
                        $highestRow = $event->sheet->getHighestRow();
                        for ($row = 2; $row <= $highestRow; $row++) {
                            if (strpos($event->sheet->getCell('B' . $row)->getValue(), 'Inquiry') !== false &&
                                strpos($event->sheet->getCell('B' . $row)->getValue(), 'Inquiry') === 0) {
                                $event->sheet->getDelegate()->getStyle('B' . $row)->getFill()
                                    ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
                                    ->getStartColor()
                                    ->setARGB('A6DCF5');
                                $event->sheet->getDelegate()->getStyle('B' . $row)->getFont()->setBold(true);
                            }
                        }
                    // Set blue background color for rows that contain the word Followup
                    $highestRow = $event->sheet->getHighestRow();
                    for ($row = 2; $row <= $highestRow; $row++) {
                        if (strpos($event->sheet->getCell('B' . $row)->getValue(), 'Followup') !== false) {
                            $event->sheet->getDelegate()->getStyle('B' . $row)->getFill()
                                ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
                                ->getStartColor()
                                ->setARGB('FFF459');
                                $event->sheet->getDelegate()->getStyle('B' . $row)->getFont()->setBold(true);
                        }
                    }

                    // Set red background color for status column that is open
                    $highestRow = $event->sheet->getHighestRow();
                    for ($row = 2; $row <= $highestRow; $row++) {
                        if (strpos($event->sheet->getCell('H' . $row)->getValue(), 'Opened') !== false) {
                            $event->sheet->getDelegate()->getStyle('H' . $row)->getFill()
                                ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
                                ->getStartColor()
                                ->setARGB('FF0000');
                                $event->sheet->getDelegate()->getStyle('H' . $row)->getFont()->setBold(true);
                                $event->sheet->getDelegate()->getStyle('H')->getFont()->getColor()->setARGB('FFFFFF');
                        }
                    }

                    // Set green background color for status column that is closed
                    $highestRow = $event->sheet->getHighestRow();
                    for ($row = 2; $row <= $highestRow; $row++) {
                        if (strpos($event->sheet->getCell('H' . $row)->getValue(), 'Closed') !== false) {
                            $event->sheet->getDelegate()->getStyle('H' . $row)->getFill()
                                ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
                                ->getStartColor()
                                ->setARGB('41B45A');
                                $event->sheet->getDelegate()->getStyle('H' . $row)->getFont()->setBold(true);
                                $event->sheet->getDelegate()->getStyle('H')->getFont()->getColor()->setARGB('FFFFFF');
                        }
                    }

                    // Set blue background color for rows that contain the word Concerns
                    $highestRow = $event->sheet->getHighestRow();
                    for ($row = 1; $row <= $highestRow; $row++) {
                        if (strpos($event->sheet->getCell('A' . $row)->getValue(), 'Concerns:') !== false) {
                                $event->sheet->getDelegate()->getStyle('A' . $row)->getFont()->setBold(true);
                        }
                    }

                    // Set blue background color for rows that contain the word Response/Actions
                    $highestRow = $event->sheet->getHighestRow();
                    for ($row = 1; $row <= $highestRow; $row++) {
                        if (strpos($event->sheet->getCell('A' . $row)->getValue(), 'Response/Actions:') !== false) {
                                $event->sheet->getDelegate()->getStyle('A' . $row)->getFont()->setBold(true);
                        }
                    }

                    $event->sheet->getDelegate()->getStyle('A1:I1')
                        ->getFont()
                        ->setBold(true);
                    $event->sheet->getDelegate()->getStyle('A1:I1')->getFont()->setSize(16);

                    // Set textwrap on the entire B column
                    $event->sheet->getDelegate()->getStyle('B:B')->getAlignment()->setWrapText(true);

                     // Set the alignment of all cells in the range A1:I1 to center
                     $event->sheet->getDelegate()->getStyle('A1:I1')->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_BOTTOM);

                    $event->sheet->getDefaultRowDimension()->setRowHeight(45);



            },

        ];
    }

     public function columnWidths(): array
        {
            return [
                'A' => 20,
                'B' => 150,
                'C' => 30,
                'D' => 30,
                'E' => 12,
                'F' => 20,
                'G' => 30,
                'H' => 10,
            ];
        }




}





 
1

There are 1 answers

0
sartis On

I found a solution: I did this loop and it worked:

for($i=1;$i<=500;$i++){
    $event->sheet->getRowDimension($i)->setRowHeight(45);
}