set background color for alternative rows

Topics: User Forum
Jan 5, 2011 at 6:42 AM

hi all,

I am creating excel using PHPExcel, there I want to set the background color for alternative rows. I have around 100 records which are fetching from the database.

Am wring the values in loop using the following code:

$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($colCount, $rowCount, $fieldValue);

For setting alternative row's background color, i tried by passing '$colCount' to getStyle, but this doesn't seems to be working. I need something like the following code as I can't directly give the column name.

              if ($row % 2 == 0) {
                    $objPHPExcel->getActiveSheet()->getStyle($colCount)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
                    $objPHPExcel->getActiveSheet()->getStyle($colCount)->getFill()->getStartColor()->setARGB('00000000');
                }

Please provide some solutions.

Thanks in advance,

INC140.

Jul 1, 2014 at 1:06 AM
Esto me funciono:
    //estilos para filas datos
    $filapar = new PHPExcel_Style();
    $filapar->applyFromArray(
        array(
            'font'  => array(
            'name'  => 'Arial', 
            'size'  => 9,              
            'color' => array(
            'rgb'   => '000000'
            )
        ),
            'fill'  => array(
            'type'  => PHPExcel_Style_Fill::FILL_SOLID,
            'color' => array('argb' =>'999999')
        ),
            'borders' => array(
            'left'    => array(
            'style'   => PHPExcel_Style_Border::BORDER_THIN ,
            'color'   => array(
            'rgb'     => '000000'
              )
           )             
        )
    ));
    $filanon = new PHPExcel_Style();
    $filanon->applyFromArray(
        array(
            'font'  => array(
            'name'  => 'Arial',
            'size'  => 9,              
            'color' => array(
            'rgb'   => '000000'
            )
        ),
            'fill'  => array(
            'type'  => PHPExcel_Style_Fill::FILL_SOLID,
            'color' => array('argb' => 'cccccc')
        ),
            'borders' => array(
            'left'    => array(
            'style'   => PHPExcel_Style_Border::BORDER_THIN ,
            'color'   => array(
            'rgb'     => '000000'
                )
            )             
        )
    ));
    $i = 4; //fila inicial de datos
    //Se agregan los datos 
    while ($fila = $rqsolbana->fetch_array()) {
        $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A'.$i,  utf8_encode($fila['desctippers']))//para datos con asento o tilde
                ->setCellValue('B'.$i,              $fila['numcli'])
                ->setCellValue('C'.$i,              $fila['folio'])
                ->setCellValue('D'.$i,  utf8_encode($fila['nomcli']))
                ->setCellValue('E'.$i,  utf8_encode($fila['calle']))
                ->setCellValue('F'.$i,  utf8_encode($fila['colonia']))
                ->setCellValue('G'.$i,  utf8_encode($fila['poblacion']))
                ->setCellValue('H'.$i,  utf8_encode($fila['estado']))
                ->setCellValue('I'.$i,              $fila['codigopostal'])
                ->setCellValue('J'.$i,              $fila['telefono1'])
                ->setCellValue('K'.$i,              $fila['telefono2'])
                ->setCellValue('L'.$i,  utf8_encode($fila['replegal']))
                ->setCellValue('M'.$i,  utf8_encode($fila['sucursal']))
                ->setCellValue('N'.$i,  utf8_encode($fila['nomsucursal']))
                ->setCellValue('O'.$i,  utf8_encode($fila['compasig']))
                ->setCellValue('P'.$i,              $fila['fec_asig'])
                ->setCellValue('Q'.$i,  utf8_encode($fila['nombre']))
                ->setCellValue('R'.$i,              $fila['fec_entrega']);

    ///*estilo para fila par o non
     if ($i % 2 == 0) { 
         $objPHPExcel->getActiveSheet()->setSharedStyle($filapar, "A".($i).":R".($i)); 
         } else { 
             $objPHPExcel->getActiveSheet()->setSharedStyle($filanon, "A".($i).":R".($i)); 
             } 
    //*/
    $i++;
    }//fin ciclo filas