previousFormat::setRightColor (Previous) (Next) Format::setBgColornext

View this page in Last updated: Sun, 18 Oct 2009
English | Brazilian Portuguese | Chinese | Dutch | French | German | Hungarian | Japanese | Polish | Russian | Spanish | Turkish

Format::setFgColor

Format::setFgColor – Sets the cell's foreground color

Synopsis

require_once "Spreadsheet/Excel/Writer.php";

void Format::setFgColor ( mixed $color )

Description

Sets the cell's "foreground color".

The term "foreground color" is misleading. Here, "foreground" means the top layer of a cell's background. To set the color of a cell's contents, use the setColor() method.

The color actually seen may depend on the pattern and background color being used.

The example entitled "How background and foreground colors interact with patterns" is very helpful.

Parameter

  • mixed $color - either a string (like 'blue'), or an integer (range is [8...63]).

    See the "Using colors" section, below, for more information.

Using colors

The following colors can be defined by name: black, white, red, green, blue, yellow, magenta and cyan.

To learn what the other indexed colors look like, read Color Palette and the 56 Excel ColorIndex Colors. Beware that the color indexes listed there are displaced by 1 with respect to those used by Spreadsheet_Excel_Writer.

If the predifined colors don't meet your requirements, use the setCustomColor() method.

Note

This function can not be called statically.

Example

Using setFgColor()

<?php
require_once 'Spreadsheet/Excel/Writer.php';

$workbook = new Spreadsheet_Excel_Writer();
$worksheet =& $workbook->addWorksheet();

// "regular" green
$format_regular_green =& $workbook->addFormat();
$format_regular_green->setFgColor('green');

// "special" green
$format_special_green =& $workbook->addFormat();
$format_special_green->setFgColor(11);

// our green (overwriting color on index 12)
$workbook->setCustomColor(121020010);
$format_our_green =& $workbook->addFormat();
$format_our_green->setFgColor(12);

$worksheet->setColumn(0030);

$worksheet->write(00"Regular green"$format_regular_green);
$worksheet->write(10"Special green (index 11)"$format_special_green);
$worksheet->write(20"Our green"$format_our_green);

$workbook->send('setFgColor.xls');
$workbook->close();
?>
previousFormat::setRightColor (Previous) (Next) Format::setBgColornext

Download Documentation Last updated: Sun, 18 Oct 2009
Do you think that something on this page is wrong? Please file a bug report or add a note.
User Notes:
Note by: mick@vandermostvanspijk.nl
Use the following script to generate an Excel file with all the colours and their indexes, so you don't have to fiddle it out yourself.

<?php
/**
 * excel_colours.php
 * 
 * Generate Excel file with the available colours and their index number
 */
require_once 'Spreadsheet/Excel/Writer.php';
$file "../../tmp/colours.xls";
$workbook = new Spreadsheet_Excel_Writer($file);
$worksheet =& $workbook->addWorksheet("Sheet1");

$worksheet->write(00"PHP PEAR Spreadsheet Excel Write Colour Index");
$offset 2;
for (
$i 0$i <= 63$i++) {
    
$color =& $workbook->addFormat(); 
    
$color->setFgColor($i);
    
$worksheet->write($i+$offset0$i$color);
}

$workbook->close();

header("Content-type:application/vnd.ms-excel");
header('Content-Disposition: attachment; filename="'.basename($file).'"');
readfile($file);
?>

Note by: gwinkless
Further to my previous note, this behaviour is actually because of bug 12062 - http://pear.php.net/bugs/bug.php?id=12062 - in fact using colors 0-7 will actually map to colors 8-15, so with the default 0.9.1beta custom colors should only start at 16.
Note by: pear@defgeoff.co.uk
custom color is listed as starting at 8, but if you change index 8 it appears to affect the color "white". Index seems to work fine if you start at 9.