aaron-bond
aaron-bond

Reputation: 3321

PHPExcel specific cell formatting from style object

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?

Upvotes: 5

Views: 26829

Answers (2)

syn
syn

Reputation: 341

You could also do this:

$objPHPExcel->getActiveSheet()
    ->getStyle('A1:B30')
    ->getFont()
    ->applyFromArray(
        array(
            'name' => 'Arial',
            'color' => array(
                'rgb' => 'FF0000'
            )
        )
    );

Upvotes: 2

Mark Baker
Mark Baker

Reputation: 212402

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']);

Upvotes: 12

Related Questions