3

I trying to export a Excel file, from a collection using laravel. The code bellow, returns me this. I need to add a 2 new rows above the start of columsn, is that possible? What Should I do?

enter image description here

<?php

namespace App\Traits;

namespace App\Exports;

// use Illuminate\Support\Collection;

use Illuminate\Database\Eloquent\Collection;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithColumnWidths;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;

use Maatwebsite\Excel\Concerns\WithCustomStartCell;


class exportSafety implements FromCollection, WithHeadings, WithMapping, WithColumnWidths, WithStyles, WithColumnFormatting, WithCustomStartCell
{
    protected $services;
    protected  $request;

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

    public function startCell(): string
    {
        return 'A3';
    }

    public function columnWidths(): array
    {
        return [
            'A' => 30,
            'B' => 20,
            'C' => 20,
            'D' => 15,
        ];
    }

    public function columnFormats(): array
    {
        return [
            'B' => NumberFormat::FORMAT_DATE_DDMMYYYY,
        ];
    }
    public function styles(Worksheet $sheet)
    {
        return [
            // Style the first row as bold text.
            3    => ['font' => ['bold' => true]],
        ];
    }
    public function collection()
    {
        $this->services->each(function ($service) {
            $this->map($service);
        });
        return $this->services;
    }

    public function headings(): array
    {
        $columns = [
            'Name' => 'Name',
            'Data de Nascimento' => 'Data de Nascimento',
            'CPF' => 'CPF',
            'Valor do Seguro' => "Valor do Seguro"
        ];
        return $columns;
    }

    public function startRow(): int
    {
        return 2;
    }

    public function map($service): array
    {
        $columns = [
            'Name' => $service->name,
            'Data de Nascimento' => $service->birthdate,
            'CPF' => $service->cpf,
            'Valor do Seguro' => $service->client->donation_safety
        ];
        return $columns;
    }
}

But I need something like this:

enter image description here

I need to put two custom row above the start of the columns, is that possible? How I to that? I am using https://docs.laravel-excel.com/3.1/imports/multiple-sheets.html

Vitor Albres
  • 113
  • 3
  • 9
  • 1
    You could create the export [from a view](https://docs.laravel-excel.com/3.1/exports/from-view.html) – Thomas Nov 24 '20 at 18:06

1 Answers1

0

you need to use WithHeadings trait to format heading rows.

$rangeHeadings = [
    'chiqaruvchi',
    'chiqarilgan',
    'sotilgan',
    "sana",
];

public function headings(): array
{
  return [
       [
          $this->product?->name . " product " . $this->params['from_date'] . ' - ' . $this->params['to_date'] . " report",
       ],
       $rangeHeadings,
     ];
}
Murod
  • 22
  • 1
  • 6