Set Font Color, Font Face and Font Size in PHPExcel

PhpClassPhpexcel

Php Problem Overview


I'm working in PHPExcel. I'm beginner.When I'm using following code and its working fine.

$phpExcel = new PHPExcel();

$phpExcel->getActiveSheet()->getStyle("A1")->getFont()->setBold(true)
								->setName('Verdana')
								->setSize(10)
							    ->getColor()->setRGB('6F6F6F');

But when I'm using following code and not getting expected result as above.

$phpFont = new PHPExcel_Style_Font();
$phpFont->setBold(true);
$phpFont->setName('Verdana');
$phpFont->setSize(15);
			
$phpColor = new PHPExcel_Style_Color();
$phpColor->setRGB('FF0000');  

$phpExcel->getActiveSheet()->getStyle('A1')->setFont( $phpFont );
$phpExcel->getActiveSheet()->getStyle('A1')->getFont()->setColor( $phpColor );

Please help me what am I doing wrong in above code.

Thank you in advance!

Php Solutions


Solution 1 - Php

I recommend you start reading the documentation (4.6.18. Formatting cells). When applying a lot of formatting it's better to use applyFromArray() According to the documentation this method is also suppose to be faster when you're setting many style properties. There's an annex where you can find all the possible keys for this function.

This will work for you:

$phpExcel = new PHPExcel();

$styleArray = array(
    'font'  => array(
        'bold'  => true,
        'color' => array('rgb' => 'FF0000'),
        'size'  => 15,
        'name'  => 'Verdana'
    ));

$phpExcel->getActiveSheet()->getCell('A1')->setValue('Some text');
$phpExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray);

To apply font style to complete excel document:

 $styleArray = array(
   'font'  => array(
        'bold'  => true,
        'color' => array('rgb' => 'FF0000'),
        'size'  => 15,
        'name'  => 'Verdana'
    ));      
 $phpExcel->getDefaultStyle()
    ->applyFromArray($styleArray);

Solution 2 - Php

PHPExcel is no longer maintained and replace by phpspreadsheet.

First read the documentation and use applyFromArray() for applying style and read the documentation of phpspreadsheet:

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$styleArray = [
        'font' => [
            'bold'  =>  true,
            'size'  =>  14,
            'name'  =>  'Arial'
        ],
        'alignment' => [
            'horizontal' => Alignment::HORIZONTAL_CENTER,
            'vertical' => Alignment::VERTICAL_CENTER
        ],
        'borders' => [
            'allBorders' => [
                'borderStyle' => Border::BORDER_THIN,
                'color' => ['rgb' => '000000']
            ]
        ]
    ];
    $sheet->getStyle('A1')->applyFromArray($styleArray);

The documentation can be found Here

Solution 3 - Php

in other way , you can use :

     $objPHPExcel->getActiveSheet()
            ->getStyle('A1')
            ->getFont()
            ->getColor()
            ->setRGB ('EEEEEE')  ;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsomView Question on Stackoverflow
Solution 1 - PhpMaxView Answer on Stackoverflow
Solution 2 - PhpHedayatullah SarwaryView Answer on Stackoverflow
Solution 3 - Phpmohammad inanlooView Answer on Stackoverflow