📌  相关文章
📜  PHP |电子表格_Excel_Writer | setBgColor()函数

📅  最后修改于: 2022-05-13 01:56:40.076000             🧑  作者: Mango

PHP |电子表格_Excel_Writer | setBgColor()函数

setBgColor()函数是PHP中的内置函数| Spreadsheet_Excel_Writer 用于设置电子表格单元格的背景颜色。要使用此函数,请使用 setPattern函数。

句法:

void Format::setBgColor( $color )

参数:此函数接受单个参数$color ,它将颜色的值作为字符串,如“红色”、“绿色”,另一种方法是指定 8 到 63 之间的颜色值。

返回值:此函数在成功时返回 TRUE,在失败时返回 PEAR_ERROR。

示例 1:

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();
?>

输出:

示例 2:

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, "GeeksforGeeks", $format_setItalic);
$worksheet->write(2, 1, "https://www.geeksforgeeks.org/", 
                                      $format_setItalic);
$workbook->send('test.xls');
  
$workbook->close();
?> 

输出:

参考: https://pear。 PHP.net/manual/en/package.fileformats.spreadsheet-excel-writer.spreadsheet-excel-writer-format.setbgcolor。 PHP