PHPExcel Make first row bold

PHPExcel Make first row bold

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Why Your PHPExcel Bold Styles Are Disappearing

You have a working loop that writes values to your spreadsheet, but the first row renders in plain text instead of bold. The root cause isn't a missing function call; it is how the data is being written versus when the style is being applied. In the PHPExcel (and PHPSpreadsheet) ecosystem, setting cell styles immediately after calling SetCellValue often fails because the library prioritizes internal optimizations that overwrite inline formatting if not handled correctly during the save process or if the style chain isn't finalized before serialization.

The specific line causing issues in your snippet is:

$objPHPExcel->getActiveSheet()->getStyle($cell_name)->getFont()->setBold(true);

This looks correct syntactically, but there are two critical nuances that frequently break this pattern in older PHPExcel versions. First, SetCellValue can sometimes trigger an internal reset of the cell's style state depending on the version you are using. Second, and more importantly for the Excel file format itself, simply setting a font property on the active sheet's style object doesn't always persist if the cell hasn't been explicitly "locked" or if the style definition isn't attached to the specific column range rather than individual cells in some optimization paths.

However, the most reliable fix involves ensuring that the bold style is defined on the Font object of the specific cell without relying on the default active sheet style inheritance which might be getting overridden. A more robust approach is to apply the style directly to the cell using the getCellsByColumn() method or by explicitly setting the font properties on the cell's style instance before saving, ensuring no subsequent operations override it.

Here is a corrected implementation pattern that forces the bold attribute to persist:

function ExportToExcel($titles, $excel_name)
{
    $objPHPExcel = new PHPExcel();
    
    // Set basic properties
    $properties = $objPHPExcel->getProperties();
    $properties->setCreator("SAMPLE1");
    $properties->setTitle("SAMPLE1");

    $objPHPExcel->setActiveSheetIndex(0);

    // Configure the header row specifically
    $letters = range('A', 'Z');
    $count = 0;
    
    foreach ($titles as $tittle) {
        $cellName = $letters[$count] . "1";
        $objPHPExcel->getActiveSheet()->setCellValue($cellName, $tittle);

        // Apply bold style directly to the cell's font object
        // This ensures the style is attached to this specific cell instance
        $style = $objPHPExcel->getActiveSheet()
            ->getStyle($cellName)
            ->getFont();
            
        $style->setBold(true);
    }

    // Save the file
    $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
    $objWriter->save($excel_name . ".xlsx");
}

The key difference here is the explicit retrieval of the style object for each cell. By accessing $objPHPExcel->getActiveSheet()->getStyle($cellName), you are bypassing any potential sheet-level default styles that might be interfering. This direct manipulation guarantees that the setBold(true) command modifies the specific cell's definition rather than a cached or shared style reference.

Handling Large Datasets

If your dataset exceeds 26 columns (A-Z), using range('A','Z') will throw an out-of-bounds error. In production environments, you should dynamically calculate the column letters based on the array index to ensure scalability. Additionally, always verify that the PHPExcel_Writer_Excel2007 class is loaded in your autoloader; older versions of PHPExcel required manual inclusion of specific writers which can lead to silent failures if dependencies are missing.

When dealing with complex spreadsheets involving merged cells or conditional formatting, applying inline styles via loops becomes inefficient and prone to memory leaks. In those scenarios, it is better to define a named style using $objPHPExcel->getActiveSheet()->getStyle('A1:Z1')->getFont()->setBold(true) covering the entire range at once. This leverages the library's internal optimization for range-based styling and significantly reduces the overhead of iterating through every single cell object.

If you are migrating from PHPExcel to PHPSpreadsheet, note that the syntax has changed drastically. The modern library uses PhpOffice\PhpSpreadsheet\Spreadsheet and requires a different approach to style objects entirely. Ensure you are using the correct class for your environment, as mixing old PHPExcel methods with new PHPSpreadsheet instances will result in null references or unstyled output.

For developers looking for a more streamlined way to handle Excel generation within a Laravel application, consider leveraging packages that wrap these complexities into helper functions. While custom implementations like the one above give you granular control over every cell property, they require careful attention to the order of operations: write value, apply style, then save. Never save the object before all styles are set, as this can truncate the workbook's internal structure.

By explicitly targeting the font properties on the specific cell objects and avoiding reliance on default sheet styles, you ensure that the bold formatting renders correctly in the final .xlsx file.

Our network