Thursday, June 18, 2026
HomeLanguagesPHP | Spreadsheet_Excel_Writer | setColor() Function

PHP | Spreadsheet_Excel_Writer | setColor() Function

The setColor() function is an inbuilt function in PHP | Spreadsheet_Excel_Writer which is used to set the color of a cell content.

Syntax:

void Format::setColor( mixed $color )

Parameters: This function accepts single parameter as $color which takes value of color as string like ‘green’ or value from 0 to 64.

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
$worksheet = &$workbook->addWorksheet('SetColor');
  
for ($inc = 0; $inc < 64; $inc++) {
  
    // Sets the color of a cell's content
    $format = $workbook->addFormat();
    $format->setColor($inc);
    $worksheet->write($inc, 0, 'Color (index '.$inc.')', $format);
}
  
// Send file to browser
$workbook->send('setColor.xls');
  
// Close workbook
$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('SetColor');
  
// Sets the color of a cell's content
$format = $workbook->addFormat();
  
// Set color to the text
$format->setColor(17);
  
// Set the size of text
$format->setSize(25);
  
// Add text to the excel
$worksheet->write(0, 5, 'GeeksForGeeks', $format);
  
// Sets the color of a cell's content
$format1 = $workbook->addFormat();
  
// Set color to the cell
$format1->setColor(2);
  
// Set the size of the text
$format1->setSize(20);
  
// Add text to the excel sheet
$worksheet->write(2, 5, 'sarthak_ishu11', $format1);
  
// Send file to browser
$workbook->send('test.xls');
  
// Close workbook
$workbook->close();
?>


Output:

Reference: https://pear.php.net/manual/en/package.fileformats.spreadsheet-excel-writer.spreadsheet-excel-writer-format.setcolor.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
6965 POSTS0 COMMENTS