How can i set existing style (->getStyle($pCellCoordinate)) to any cell in any worksheet

Topics: Developer Forum
Jul 31, 2012 at 6:14 AM

There is a excellent method PHPExcel_Worksheet->getStyle($pCellCoordinate). But i'm do not find any method like "setStyle"

Coordinator
Jul 31, 2012 at 6:34 AM
Edited Jul 31, 2012 at 6:37 AM

getStyle returns a style object. That style object has getter methods for all the style features such as font, borders, fill, protection, number masking, and those style features have setter methods:

e.g.

$objPHPExcel->getActiveSheet()->getStyle('D1')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX15);
$objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setName('Arial');

Alternatively, you can build an array detailing all the style elements you want to set, and use the style object's applyFromArray() method

$styleThinBlackBorderOutline = array(
 'borders' => array(
  'outline' => array(
   'style' => PHPExcel_Style_Border::BORDER_THIN,
   'color' => array('argb' => 'FF000000'),
  ),
 ),
);
$objPHPExcel->getActiveSheet()->getStyle('A4:E10')->applyFromArray($styleThinBlackBorderOutline);

There are also methods in the Worksheet object such as setSharedStyle(), duplicateStyle() and duplicateStyleArray() that allow you to set styles to a range of cells rather than just to individual cells, or to duplicate the style set against a cell to a whole range of cells.

 

All this is fully detailed in the documentation and in the examples