moktar's avatar

laravel excel multiple sheets with heading

Hello , i have a problem with laravel-excel package, when exporting some data to an excel file for multiple sheets the heading don't show up , before that it was working before was only one sheet . any one has faced the same problem before ?? ican't find an answer in docs , it toke from a long time to find a solution , but nothing yet .

the package : https://laravel-excel.com

0 likes
12 replies
cometads's avatar

Can you show your code for the parent sheet and one of your children sheet classes?

1 like
moktar's avatar

export method in controller:

    public function exportCsv(Request $request)
    {
        try {
            $fileName = str_random(10) . "" . "claims.xlsx";

            (new ClaimsExport)->queue("/public/" . $fileName)->chain([
                new NotifyUserOfCompletedExport($request->user(), $fileName),
            ]);

            return $this->respondWithSuccess(200);
        } catch (\Exception $e) {
            $this->setStatusCode(500);
            return $this->respondWithError($e->getMessage());
        }
    }

claims export

use Exportable;

    public function sheets(): array
    {
        $sheets = [];
        $activities = Activity::where("name", "WG")->first();

        foreach ($activities->teams as $team) {
            $sheets[] = new ClaimsByTeamsExport($team);
        }

        return $sheets;
    }
}

claims export by teams

    private $team;

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

    public function query()
    {
        return Claim::query()->where('team_id', $this->team->id);
    }

    public function headings(): array
    {

        return [
            '#',
            'Agent Name',
        ];
    }

    public function map($claim): array
    {
        return [
            $claim->id,
            $claim->user->name,
        ];
    }

    public function title(): string
    {
        return $this->team->name;
    }
cometads's avatar

That all looks correct assuming your use and implements are there.

The sheets are generated just not named correctly right? What are the sheets being named?

moktar's avatar

the sheets named correctly as the database , ex : team 1 , team 2 , team 3 ....

cometads's avatar

Ohhhh. Apologies. Misunderstood your issue. Thought you meant title of the worksheets, not column headings.

So rephrasing of my first question, are you implementing WithHeadings in the child classes?

moktar's avatar

yes , no worries, i dont get the heading of sheets , and the relations this the problem , do you think it because they are separated ??

moktar's avatar

about implementing yes i implemented all stuff

cometads's avatar
cometads
Best Answer
Level 3

I'm not sure what the issue is then. Here's an example of multiple sheets with headings working in one of my projects. Maybe you can use it to compare/contrast and find the issue.

Controller:

...
$export = new ReportExport($data['data']);

return Excel::download($export, 'report.xlsx');

Parent sheet:

<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;

class ReportExport implements FromArray, WithMultipleSheets
{
    protected $sheets;

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

    public function array(): array
    {
        return $this->sheets;
    }

    public function sheets(): array
    {
        $sheets = [
            new ReportGeneralExport($this->sheets['general']),
            new ReportLeadsExport($this->sheets['leads']),
            new ReportVideoExport($this->sheets['video'])
        ];

        return $sheets;
    }
}

Child sheet:

<?php
namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use Maatwebsite\Excel\Concerns\WithMapping;

class ReportGeneralExport implements FromArray, WithHeadings, WithTitle, ShouldAutoSize, WithColumnFormatting, WithMapping
{
    protected $rows;

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

    public function map($row): array
    {
        return [
            $row['name'],
            $row['impressions'],
            $row['clicks'],
            $row['ctr']
        ];
    }

    public function headings(): array
    {
        return [
            'Name',
            'Impressions',
            'Clicks',
            'CTR'
        ];
    }

    public function array(): array
    {
        return $this->rows;
    }

    public function title(): string
    {
        return 'General';
    }

    public function columnFormats(): array
    {
        return [
            'B' => '#,##0',
            'C' => '#,##0',
            'D' => NumberFormat::FORMAT_PERCENTAGE_00
        ];
    }
}

Hope this helps.

4 likes
moktar's avatar

thanks i really appreciate, your reply

1 like
engdoaa's avatar

i try to use this code but there's problem that i can't understand what you mean by data here ??? $export = new ReportExport($data['data']);

1 like

Please or to participate in this conversation.