8

I am setting a cell value in phpexcel using below method setCellValueByColumnAndRow()

$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col,$xlsRow,$plan);

Now my requirment is to set background color for this.

I am not able to use this below method as I am aligned with rows and columns numbers.

 $objPHPExcel->getActiveSheet()->getStyle("A1")->getFill()
    ->setFillType(PHPExcel_Style_Fill::FILL_SOLID)
    ->getStartColor()->setRGB($color);

I am searching a way to provide cols and rows as (2,3) not like ('A1:E1')

Please suggest an alternative way to set background color using column and row numbers.

ManiMuthuPandi
  • 1,594
  • 2
  • 26
  • 46

2 Answers2

9

PHPExcel_Cell::stringFromColumnIndex(0); worked perfectly.

$column = PHPExcel_Cell::stringFromColumnIndex(45);
$row = 1;
$cell = $column.$row;

The $cell will give you AT1 $range = 'A1:'.$cell; So you can easily pass into the filling range like.

$objPHPExcel->getActiveSheet()->getStyle($range)->getFill()->applyFromArray(array(
        'type' => PHPExcel_Style_Fill::FILL_SOLID,
        'startcolor' => array(
             'rgb' => 'FFFF00' //Yellow
        )
    ));
Zia
  • 131
  • 1
  • 6
5

You cannot style a row in PHPExcel, only a cell or a range of cells

$objPHPExcel->getActiveSheet()
    ->getStyle('A1:E1')
    ->getFill()
    ->setFillType(PHPExcel_Style_Fill::FILL_SOLID)
    ->getStartColor()
    ->setARGB('FF808080');

or

$objPHPExcel->getActiveSheet()
    ->getStyle('A1:E1')
    ->applyFromArray(
        array(
            'fill' => array(
                'type' => PHPExcel_Style_Fill::FILL_SOLID,
                'color' => array('rgb' => 'E05CC2')
            )
        )
    );

Will set the background fill style for cells A1 to E1

Jimish Gamit
  • 1,014
  • 5
  • 15
  • Thanks for your answer. I need to set background using rows and cols numbers like (0,1) or (1,1),.. I am not able to have that Alpha index (A1:E1) – ManiMuthuPandi Feb 08 '16 at 06:51
  • 2
    use `PHPExcel_Cell::stringFromColumnIndex` to get Alpha index `// result = 'A' $columnLetter = PHPExcel_Cell::stringFromColumnIndex(0); ` – Jimish Gamit Feb 08 '16 at 07:02