set bgcolor

Topics: User Forum
Sep 25, 2009 at 5:32 PM

How to set the bgcolor for the cell (A1)?

ini_set('include_path', ini_get('include_path').';../Classes/');
include 'PHPExcel.php';
include 'PHPExcel/Writer/Excel5.php';


$title = array(
    'font'    => array(
        'name'      => 'Arial',
        'size'        => 14,
        'bold'      => true,
        'italic'    => false,
        'underline' => false,
        'strike'    => false,
        'color'     => array(
            'rgb' => '00FF00'
        )
    ),
    'borders' => array(
        'bottom'     => array(
            'style' => PHPExcel_Style_Border::BORDER_THIN,
            'color' => array(
                'rgb' => '000000'
            )
        ),
        'right'     => array(
            'style' => PHPExcel_Style_Border::BORDER_THIN,
            'color' => array(
                'rgb' => '000000'
            )
        )
    )
);

$objPHPExcel = new PHPExcel();

$objPHPExcel->setActiveSheetIndex(0);
$ActiveSheet = $objPHPExcel->getActiveSheet();
$ActiveSheet->setTitle('page 1');
$ActiveSheet->SetCellValue('A1', 'Title');
$ActiveSheet->getStyle('A1')->applyFromArray($title);
$ActiveSheet->mergeCells('A1:B1');

Developer
Sep 25, 2009 at 11:02 PM

require_once 'Classes/PHPExcel.php';
require_once 'Classes/PHPExcel/IOFactory.php';

$title = array(
    'font' => array(
        'name' => 'Arial',
        'size' => 14,
        'bold' => true,
        'color' => array(
            'rgb' => '00FF00'
        ),
    ),
    'borders' => array(
        'bottom' => array(
            'style' => PHPExcel_Style_Border::BORDER_THIN,
            'color' => array(
                'rgb' => '000000'
            )
        ),
        'right' => array(
            'style' => PHPExcel_Style_Border::BORDER_THIN,
            'color' => array(
                'rgb' => '000000'
            )
        )
    ),
    'fill' => array(
        'type' => PHPExcel_Style_Fill::FILL_SOLID,
        'startcolor' => array(
            'rgb' => 'FFDD00',
        ),
    ),
);

$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$ActiveSheet = $objPHPExcel->getActiveSheet();
$ActiveSheet->setTitle('page 1');
$ActiveSheet->SetCellValue('A1', 'Title');
$ActiveSheet->getStyle('A1:B1')->applyFromArray($title);
$ActiveSheet->mergeCells('A1:B1');

$writer = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$writer->save('write.xls');

 

Additional notes:

  • PHPExcel 1.7.0 no longer requires an include path to be set up. Just include the classes you are using, that's enough.
  • When you style merged cells you are required to apply the style to all the cells participating in the merge. Otherwise you will get some funny results with borders. Look at the example.
Sep 28, 2009 at 2:27 PM

Thank you.