Wednesday, June 17, 2026
HomeLanguagesPHP | Spreadsheet_Excel_Writer | setFgColor() Function

PHP | Spreadsheet_Excel_Writer | setFgColor() Function

The setFgColor() function is an inbuilt function in PHP | Spreadsheet_Excel_Writer which is used to set the foreground color for the cell of the spreadsheet. In order to use this function use the setPattern function. Here, “foreground” means the top layer of a cell’s background.

Syntax:

void Format::setFgColor( $color )

Parameters: This function accepts single parameter $color which takes the value of color as string like ‘red’, ‘green’ and another method by specifying the color codes between 8 to 63.

Return Value: This function returns TRUE on success and PEAR_ERROR on failure.

Example 1:




<?php
  
require_once 'Spreadsheet/Excel/Writer.php';
  
// Create Spreadsheet Excel Writer Object
$workbook = new Spreadsheet_Excel_Writer();
  
// Add Worksheet to the spreadsheet
$worksheet =& $workbook->addWorksheet();
  
// Add "regular" green to the cell
$format_regular_green =& $workbook->addFormat();
$format_regular_green->setFgColor('green');
  
// Add "special" green to the cell
$format_special_green =& $workbook->addFormat();
$format_special_green->setFgColor(11);
  
// Set Custom Green Color
$workbook->setCustomColor(12, 10, 200, 10);
$format_our_green =& $workbook->addFormat();
$format_our_green->setFgColor(12);
  
// Set Column width
$worksheet->setColumn(0, 0, 30);
  
// Add data to the spreadsheet 
$worksheet->write(0, 0, "neveropen", $format_regular_green);
$worksheet->write(1, 0, "Sarthak Prajapati", $format_special_green);
$worksheet->write(2, 0, "sarthak_ishu11", $format_our_green);
  
// Send file to the browser
$workbook->send('setFgColor.xls');
  
// Close the file 
$workbook->close();
?>


Output:

Example 2:




<?php
  
require_once 'Spreadsheet/Excel/Writer.php';
  
// Create Spreadsheet_Excel_Writer Object
$workbook = new Spreadsheet_Excel_Writer();
  
// Add Worksheet
$worksheet =& $workbook->addWorksheet();
  
// Set Font Family Times New Roman 
$format_setFgColor =& $workbook->addFormat();
$format_setFgColor->setFontFamily('Times New Roman');
  
// Set Italic Property
$format_setFgColor->setItalic();
  
// Set Shadow to text
$format_setFgColor->setShadow();
  
// Set the foreground color 
$format_setFgColor->setBgColor('yellow');
  
// Set the pattern
$format_setFgColor->setPattern(4);
  
// Write to Worksheet
$worksheet->write(0, 0, "Information", $format_setFgColor);
$worksheet->write(1, 0, "Website Name", $format_setFgColor);
$worksheet->write(1, 1, "Address", $format_setFgColor);
$worksheet->write(2, 0, "neveropen", $format_setFgColor);
$worksheet->write(2, 1, "https://www.geeksforgeeks.org/", $format_setFgColor);
$workbook->send('test.xls');
  
$workbook->close();
?> 


Output:

Reference: https://pear.php.net/manual/en/package.fileformats.spreadsheet-excel-writer.spreadsheet-excel-writer-format.setfgcolor.php

RELATED ARTICLES

Most Popular

Dominic
32516 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6898 POSTS0 COMMENTS
Nicole Veronica
12014 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS