0

How can I configure the first row of my Excel document as a header row?

I can only find ways to customize the style of the cells in the first row. For this I made myself a little helper method, but there must be a flag I can set, right?

What I'm currently using as a "workaround":

/**
 * Applies the header style (bold font, and gray bg) to the given row in a sheet.
 * 
 * @param Worksheet $sheet
 * @param int $row
 * 
 * @return Worksheet
 */
private function applyHeaderStyle(Worksheet $sheet, int $row = 1): Worksheet
{
    $highestColumn = $sheet->getHighestColumn();
    $sheet
        ->getStyle("A{$row}:{$highestColumn}{$row}")
        ->getFont()
        ->setBold(true);
    $sheet
        ->getStyle("A{$row}:{$highestColumn}{$row}")
        ->getFill()
        ->setFillType(Fill::FILL_SOLID)
        ->getStartColor()
        ->setARGB('FFBEC0BF');
    return $sheet;
}

But this will apply only styles and only to a limited set of cells and not the infinite length of the row.

Update:

Getting the style like ->getStyle("{$row}:{$row}") and applying a bold font and background color to it, like proposed in this answer, works for the font style but not for the background color.

Apart from that, I still wonder if an Excel header is really "only" marked by its design, or if there is a flag for it.

floriankapaun
  • 472
  • 1
  • 4
  • 19
  • Excel uses Range("1:1") to format the entire row, and looking here: https://stackoverflow.com/a/25673930/212869 it might be possible to do it with phpspreadsheet too? – NickSlash Oct 28 '22 at 06:33
  • I've actually seen the linked resource @NickSlash, but it's only working for the font. If I try to apply the background color to range("1:1") some weird stuff is going on and only the last cell gets the specified background color applied. – floriankapaun Oct 28 '22 at 07:42

0 Answers0