Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set cell background using column and row index

Tags:

php

phpexcel

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.

like image 841
ManiMuthuPandi Avatar asked Feb 08 '16 06:02

ManiMuthuPandi


2 Answers

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
        )
    ));
like image 81
Zia Avatar answered Nov 05 '22 18:11

Zia


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

like image 31
Jimish Gamit Avatar answered Nov 05 '22 18:11

Jimish Gamit