PHPExcel color to specific row

user4433485 picture user4433485 · Jan 16, 2015 · Viewed 44k times · Source

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;)

Answer

Mark Baker picture Mark Baker · Jan 16, 2015

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