96

How to set specific color to active cell when creating XLS document in PHPExcel?

ariefbayu
  • 21,849
  • 12
  • 71
  • 92
user198003
  • 11,029
  • 28
  • 94
  • 152

10 Answers10

156
$sheet->getStyle('A1')->applyFromArray(
    array(
        'fill' => array(
            'type' => PHPExcel_Style_Fill::FILL_SOLID,
            'color' => array('rgb' => 'FF0000')
        )
    )
);

Source: http://bayu.freelancer.web.id/2010/07/16/phpexcel-advanced-read-write-excel-made-simple/

Ross
  • 46,186
  • 39
  • 120
  • 173
user198003
  • 11,029
  • 28
  • 94
  • 152
84
function cellColor($cells,$color){
    global $objPHPExcel;

    $objPHPExcel->getActiveSheet()->getStyle($cells)->getFill()->applyFromArray(array(
        'type' => PHPExcel_Style_Fill::FILL_SOLID,
        'startcolor' => array(
             'rgb' => $color
        )
    ));
}

cellColor('B5', 'F28A8C');
cellColor('G5', 'F28A8C');
cellColor('A7:I7', 'F28A8C');
cellColor('A17:I17', 'F28A8C');
cellColor('A30:Z30', 'F28A8C');

enter image description here

Alexxus
  • 893
  • 1
  • 11
  • 25
Limitless isa
  • 3,689
  • 36
  • 28
  • 6
    Your functions it's ok, but you are using global, and that's a real error... You should make use of the PHP5 features. Instead, you could try a lambda function such as https://eval.in/39136 :) – Cito Jul 26 '13 at 15:06
38

This code should work for you:

 $PHPExcel->getActiveSheet()
        ->getStyle('A1')
        ->getFill()
        ->setFillType(PHPExcel_Style_Fill::FILL_SOLID)
        ->getStartColor()
        ->setRGB('FF0000')

But if you bother using this over and over again, I recommend using applyFromArray.

Muntashir Akon
  • 8,740
  • 2
  • 27
  • 38
11

Seems like there's a bug with applyFromArray right now that won't accept color, but this worked for me:

$objPHPExcel
    ->getActiveSheet()
    ->getStyle('A1')
    ->getFill()
    ->getStartColor()
    ->setRGB('FF0000');
jocull
  • 20,008
  • 22
  • 105
  • 149
  • 1
    Though it is more elegant solution (as I prefer OOP way) it didn't worked for me :/ Used @user198003 provided solution – Aurimas Aug 15 '14 at 11:29
11

This always running!

$sheet->getActiveSheet()->getStyle('A1')->getFill()->getStartColor()->setRGB('FF0000');

Rogerio de Moraes
  • 1,527
  • 18
  • 15
  • 2
    In some php version don't can break line at attribute class. Old version. – Rogerio de Moraes Jan 05 '15 at 10:55
  • 3
    **IMPORTANT! Than $objPHPExcel like $sheet is a define from object class PHPExcel. You need use like you instantiated (at new PHPExcel() define).** – Rogerio de Moraes Jan 08 '15 at 10:29
  • 1
    $objPHPExcel->getActiveSheet()->getStyle('A'.$row.':G'.$row)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB('FF0000'); – Defkon1 Sep 25 '18 at 12:16
10

Here is how you do it in PHPSpreadsheet, the newest version of PHPExcel

$spreadsheet = new Spreadsheet();

$spreadsheet->getActiveSheet()->getStyle('A1:F1')->applyFromArray([
    'fill' => [
            'fillType' => Fill::FILL_SOLID,
            'startColor' => [
                'rgb' => 'FFDBE2F1',
            ]           
    ],
]);

alternative approach:

$spreadsheet->getActiveSheet()
    ->getStyle('A1:F1')
    ->getFill()
    ->setFillType(Fill::FILL_SOLID)
    ->getStartColor()->setARGB('FFDBE2F1');
hous
  • 2,577
  • 2
  • 27
  • 66
deerawan
  • 8,002
  • 5
  • 42
  • 51
4
$objPHPExcel
->getActiveSheet()
->getStyle('A1')
->getFill()
->setFillType(PHPExcel_Style_Fill::FILL_SOLID)
->getStartColor()
->setRGB('colorcode'); //i.e,colorcode=D3D3D3
Vatsal Patel
  • 317
  • 5
  • 15
1
$objPHPExcel
    ->getActiveSheet()
    ->getStyle('A1')
    ->getFill()
    ->getStartColor()
    ->getRGB();
J. Steen
  • 15,470
  • 15
  • 56
  • 63
Abhishek Jaiswal
  • 2,524
  • 1
  • 21
  • 24
0
  $objPHPExcel->getActiveSheet()->getStyle('B3:B7')->getFill()
->setFillType(PHPExcel_Style_Fill::FILL_SOLID)
->getStartColor()->setARGB('FFFF0000');

It's in the documentation, located here: https://github.com/PHPOffice/PHPExcel/wiki/User-Documentation-Overview-and-Quickstart-Guide

ThatTechGuy
  • 879
  • 1
  • 10
  • 29
0

You can easily apply colours on cell and rows.

$sheet->cell(1, function($row) 
{ 
  $row->setBackground('#CCCCCC'); 
});

$sheet->row(1, ['Col 1', 'Col 2', 'Col 3']); 
$sheet->row(1, function($row) 
{ 
  $row->setBackground('#CCCCCC'); 
});
pankaj
  • 31
  • 1
  • 2