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?
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']);
You could also do this:
$objPHPExcel->getActiveSheet()
->getStyle('A1:B30')
->getFont()
->applyFromArray(
array(
'name' => 'Arial',
'color' => array(
'rgb' => 'FF0000'
)
)
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With