Thursday, November 20, 2025
HomeLanguagesPHP | Spreadsheet_Excel_Writer | setBgColor() Function

PHP | Spreadsheet_Excel_Writer | setBgColor() Function

The setBgColor() function is an inbuilt function in PHP | Spreadsheet_Excel_Writer which is used to set the background color of the cell of the spreadsheet. In order to use this function use the setPattern function.

Syntax:

void Format::setBgColor( $color )

Parameters: This function accepts single parameter $color which takes the value of color as string like ‘red’, ‘green’ and another method is by specifying the color value 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 
$worksheet =& $workbook->addWorksheet('testing bg color');
  
// Specify the height of the cell by using setRow function
$worksheet->setRow(1, 20);
  
// Add Format to the cell
$format_BgColor =& $workbook->addFormat();
  
// Set the background color 
$format_BgColor->setBgColor('red');
  
// Add Italic Font to the text
$format_BgColor->setItalic();
  
// Set the pattern
$format_BgColor->setPattern(4);
  
// Add data to the cell 
$worksheet->write(1, 2, 'sarthak _ishu11', $format_BgColor);
$worksheet->write(2, 2, 'GeekforGeeks!', $format_BgColor);
$worksheet->write(3, 2, 'GfG', $format_BgColor);
  
// Send file to the browser
$workbook->send('test.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_setBgColor =& $workbook->addFormat();
$format_setBgColor->setFontFamily('Times New Roman');
  
// Set Italic Property
$format_setBgColor->setItalic();
  
// Set Shadow to text
$format_setBgColor->setShadow();
  
// Set the background color 
$format_setBgColor->setBgColor('yellow');
  
// Set the pattern
$format_setBgColor->setPattern(4);
  
// Write to Worksheet
$worksheet->write(0, 0, "Information", $format_setBgColor);
$worksheet->write(1, 0, "Website Name", $format_setBgColor);
$worksheet->write(1, 1, "Address", $format_setBgColor);
$worksheet->write(2, 0, "neveropen", $format_setItalic);
$worksheet->write(2, 1, "https://www.geeksforgeeks.org/", 
                                      $format_setItalic);
$workbook->send('test.xls');
  
$workbook->close();
?> 


Output:

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

RELATED ARTICLES

Most Popular

Dominic
32404 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6776 POSTS0 COMMENTS
Nicole Veronica
11925 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11994 POSTS0 COMMENTS
Shaida Kate Naidoo
6905 POSTS0 COMMENTS
Ted Musemwa
7160 POSTS0 COMMENTS
Thapelo Manthata
6861 POSTS0 COMMENTS
Umr Jansen
6846 POSTS0 COMMENTS