Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPExcel specific cell formatting from style object

Tags:

php

phpexcel

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?

like image 665
aaron-bond Avatar asked Oct 16 '12 15:10

aaron-bond


2 Answers

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']);
like image 142
Mark Baker Avatar answered Nov 14 '22 00:11

Mark Baker


You could also do this:

$objPHPExcel->getActiveSheet()
    ->getStyle('A1:B30')
    ->getFont()
    ->applyFromArray(
        array(
            'name' => 'Arial',
            'color' => array(
                'rgb' => 'FF0000'
            )
        )
    );
like image 38
syn Avatar answered Nov 14 '22 02:11

syn