PHPExcel specific cell formatting from style object

PHPphpexcel

I'm using PHPExcel in a project and need to style the cells of the excel sheets.

What I've done is create a PHPExcel style object like this:

$style['red_text'] = new PHPExcel_Style();

I then use the set functions on this style to fill up the object like this:

$style['red_text']->getFont()
        ->applyFromArray(
                array('name'=>'Arial')
         )

Now I am trying to use this style object in a cell. I tried to use the applyFromArray function like this:

$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($x, $y)->applyFromArray( $style['red_text'] );

This isn't the way to do it I don't think. To me, this is the most readable and consistent way of assigning the styles but if someone who is more fluent with PHPExcel could direct me towards the proper method I'd be much obliged!

P.S. Excuse the formatting; this is my first post ๐Ÿ™‚

EDIT: just found the error in this: "Invalid style array passed"
Does this mean I'm creating the style object wrongly?

Best Answer

Applying from array is literally applying from an array, not from a style object

$style['red_text'] = array(
    'font' => array(
        'name' => 'Arial',
        'color' => array(
            'rgb' => 'FF0000'
        )
    ),
);
$objPHPExcel->getActiveSheet()
    ->getStyleByColumnAndRow($x, $y)
    ->applyFromArray($style['red_text']);

or alternatively:

$style['red_text'] = array(
    'name' => 'Arial',
    'color' => array(
        'rgb' => 'FF0000'
    )
);
$objPHPExcel->getActiveSheet()
    ->getStyleByColumnAndRow($x, $y)
    ->getFont()
    ->applyFromArray($style['red_text']);
Related Topic