phpphpspreadsheet

PHP-Spreadsheet: First Row Bold


Because PHP-Excel is deprecated, PHP-Spreadsheet is the new Option to make Excel in PHP. The Documentation is not the best and the're not many questions on Stackoverflow about PHP-Spreadsheet, so that's why I make this Question about how to make the first row bold in PHP-Spreadsheet.


Solution

  • This should do the trick.

    //Create Spreadsheet
    $spreadsheet = new Spreadsheet();
    $sheet = $spreadsheet->getActiveSheet();
    
    //Create Styles Array
    $styleArrayFirstRow = [
                'font' => [
                    'bold' => true,
                ]
            ];
    
    //Retrieve Highest Column (e.g AE)
    $highestColumn = $sheet->getHighestColumn();
    
    //set first row bold
    $sheet->getStyle('A1:' . $highestColumn . '1' )->applyFromArray($styleArrayFirstRow);