How to change font of a row or a specific cell in a generated Excel through PHPExcel

15,889

I'm assuming you're using the latest version (v1.8.0). You have different options depending on the outcome you want to achieve:

To set the style for the entire workbook:

$phpExcel->getDefaultStyle()->getFont()
    ->setName('Arial')
    ->setSize(14)
    ->setBold(true);

To apply a custom style to a single cell (or a range of cells):

$phpExcel->getActiveSheet()->getStyle('A3')->applyFromArray($styleArray);

Here is an example:

$phpExcel = new PHPExcel();

// set the font style for the entire workbook
$phpExcel->getDefaultStyle()->getFont()
    ->setName('Arial')
    ->setSize(14)
    ->setBold(true);

// custom style with different font than the global one
$styleArray = array(
    'font'  => array(
        'bold' => true,
        'color' => array('rgb' => 'FF0000'),
        'size'  => 15,
        'name' => 'Verdana'
    ));

// create some cells
$phpExcel->getActiveSheet()->getCell('A1')->setValue('Hello world');
$phpExcel->getActiveSheet()->getCell('A2')->setValue('Hello again');
$phpExcel->getActiveSheet()->getCell('A3')->setValue('Goodbye');

// apply custom style to single cell
$phpExcel->getActiveSheet()->getStyle('A3')->applyFromArray($styleArray);

And the result will be something like this (notice the first cells have the global style while the A3 cell has the custom style):

enter image description here

Share:
15,889
Steve
Author by

Steve

I make funny faces.

Updated on June 05, 2022

Comments

  • Steve
    Steve almost 2 years

    I am using PHPExcel to create an Excel but unfortunately I am unable to change font of a specific cell or a specific row. I am even unable to make it bold.

    I did anyhow change the entire Excel's font and size through

    $objPHPExcel->getDefaultStyle()->getFont()->setName('Arial');
    $objPHPExcel->getDefaultStyle()->getFont()->setSize(14);
    

    I tried this but nothing seems to work:-

    $objPHPExcel->getActiveSheet()->getComment('A1')->getFont()->setBold(true);
    

    Also any idea how I can increase the Series' font of a chart?

    I'll be thankful of any assistance.

    Best