13

I am currently working with PHPExcel and I am trying to give 1 specific row a color, I have read Set Background cell color in PHPExcel already and I have try all of those options. Without luck!

Maybe I do something wrong, but here it goes:

           //Table
    $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('B2', 'Beller ')
                ->setCellValue('D2', 'Beller nummer')
                ->setCellValue('F2', 'Datum')
                ->setCellValue('H2', 'ontvanger naam')
                ->setCellValue('J2', 'ontvanger nummer')
                ->setCellValue('L2', 'Billing seconds')
                ->setCellValue('N2', 'Direction')
    // array 

                ->setCellValue('B3', 'hi')
                ->setCellValue('D3', 'hi')
                ->setCellValue('F3', 'hi')
                ->setCellValue('H3', 'hi')
                ->setCellValue('J3', 'ontvanger nummer')
                ->setCellValue('L3', 'Billing seconds')
                ->setCellValue('N3', 'Direction');          

                //Werkt niet            }


    // breedte + kopstukken dikgedrukt
    $objPHPExcel->getActiveSheet()->getStyle('B2')->getFont()->setBold(true);
    $objPHPExcel->getActiveSheet()->getStyle('D2')->getFont()->setBold(true);
    $objPHPExcel->getActiveSheet()->getStyle('F2')->getFont()->setBold(true);
    $objPHPExcel->getActiveSheet()->getStyle('H2')->getFont()->setBold(true);
    $objPHPExcel->getActiveSheet()->getStyle('J2')->getFont()->setBold(true);
    $objPHPExcel->getActiveSheet()->getStyle('L2')->getFont()->setBold(true);
    $objPHPExcel->getActiveSheet()->getStyle('N2')->getFont()->setBold(true);
    $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
    $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(15);
    $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
    $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(15);
    $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(20);
    $objPHPExcel->getActiveSheet()->getColumnDimension('L')->setWidth(15);
    $objPHPExcel->getActiveSheet()->getColumnDimension('N')->setWidth(15);
    $objPHPExcel->getActiveSheet()->getStyle('N2')->applyFromArray(
        array(
            'fill' => array(
                'type' => PHPExcel_Style_Fill::FILL_SOLID,
                'color' => array('rgb' => 'E05CC2')
            )
        )

);

it gives me N2 colored, but I want all the rows in N filled with text, be colored;)

Community
  • 1
  • 1
  • Have you tried with `applyFromArray` method ? – Brewal Jan 16 '15 at 10:17
  • I just did now, and it worked but not for all rows only for N2, I actually want all of the FILLED in N to be collored editing my post –  Jan 16 '15 at 10:18
  • also, add this before your very last line: `$objPHPExcel->getActiveSheet()->getStyle('N2')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);` – briosheje Jan 16 '15 at 10:19
  • I removed that, since it didn't give me anything, using the array method wich gives me 1 row colored –  Jan 16 '15 at 10:25

1 Answers1

38

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

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • The thing is, its working with datepick results, if someone picks a date, it returns all call history from that date, from a variable wich I use in a array, the thing is that I must color all the filled cells on row N –  Jan 16 '15 at 10:28
  • All the __filled__ cells? or all the cells? – Mark Baker Jan 16 '15 at 10:29
  • All cells from N wich are filled with the data from the array. so imagine, they load call history from yesterday and it has 12 calls, I want 12cells colored in N, –  Jan 16 '15 at 10:29
  • In that case, you work out the range of those values and use the method I've shown above; so if your starting cell is `N2` and you have 20 values, then the range will be `N2:N21` – Mark Baker Jan 16 '15 at 10:30
  • How can I know where to stop the coloring? since it change on what date user picks and based on how many calls there have been since the picked date, Could I use the count() function? –  Jan 16 '15 at 10:31
  • PHPExcel can't tell you that, your code logic has to determine that – Mark Baker Jan 16 '15 at 10:32
  • Though you could always use conditional formatting with a test against the value in cells, and only change the colour if the cell value matches your specified date value – Mark Baker Jan 16 '15 at 10:32
  • Thank you, I will figure something out, will try with a count() function and turn the int into a variable and ->getStyle('N2:$VarCount') –  Jan 16 '15 at 10:34
  • can i set cell color when i writing cell value? like $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($coll, $roww, 'A'); – Divyesh Jesadiya Jun 11 '16 at 15:52