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

Rretzko's avatar
Level 15

Maatwebsite/Excel Multiple Headers not working

Hi - I have an Excel export which works beautifully and I'm trying to include multiple column header rows. When I use a single header row, it works:

use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;

class CombinedConfidentialsExport implements FromArray, WithHeadings
{
	public function headings(): array
    {
/*
$header3 [
  0 => "aud #"
  1 => "school"
  2 => "vp"
  3 => "total"
  4 => "result"
  5 => 1
  6 => 2
  7 => 3
  8 => 4
  9 => 5
  10 => 6
  11 => 7
  12 => 8
  13 => 9
  14 => 10
  15 => 11
  16 => 12
  17 => 13
  18 => 14
  19 => 15
  20 => 16
  21 => 17
  22 => 18
]
*/
       $header3 = $this->getColumnHeaders3();

        return [         
            $header3,
        ];
    }
}

When I include $header2, neither header prints

/*
$header2 [
  0 => ""
  1 => ""
  2 => ""
  3 => ""
  4 => ""
  5 => "lms"
  6 => "hms"
  7 => "cms"
  8 => "arp"
  9 => "solo"
  10 => "qrt"
  11 => "lms"
  12 => "hms"
  13 => "cms"
  14 => "arp"
  15 => "solo"
  16 => "qrt"
  17 => "lms"
  18 => "hms"
  19 => "cms"
  20 => "arp"
  21 => "solo"
  22 => "qrt"
]
*/
 return [
		$header2,         
        $header3,
        ];

Any thoughts on troubleshooting this are appreciated!

0 likes
1 reply
LaryAI's avatar
Level 58

It seems like you're trying to include multiple header rows in your Excel export using the Maatwebsite/Excel package. The code you've provided should work for multiple headers as long as both $header2 and $header3 are defined and returned as an array of arrays from the headings method.

Here's a revised version of your headings method that should work for multiple headers:

use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;

class CombinedConfidentialsExport implements FromArray, WithHeadings
{
    public function headings(): array
    {
        $header2 = [
            // ... your header2 values here
        ];

        $header3 = [
            // ... your header3 values here
        ];

        return [
            $header2,
            $header3,
        ];
    }
}

Make sure that both $header2 and $header3 are properly defined arrays before returning them. If you're still facing issues, here are a few troubleshooting steps:

  1. Check for Errors: Make sure there are no syntax errors or typos in your code.
  2. Debugging: Add dd($header2, $header3); right before the return statement to dump the headers and ensure they are what you expect.
  3. Package Version: Ensure you are using a version of the Maatwebsite/Excel package that supports multiple headers. If you're using an older version, consider updating.
  4. Documentation: Review the official documentation for any updates or changes in how to handle multiple headers.

If you've followed these steps and are still encountering issues, please provide more context or any error messages you're receiving, and I'll be happy to help you further.

Please or to participate in this conversation.