當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


PHP Spreadsheet_Excel_Writer setFgColor()用法及代碼示例


setFgColor()函數是PHP中的內置函數| Spreadsheet_Excel_Writer,用於設置電子表格單元格的前景色。為了使用此函數,請使用setPattern函數。在這裏,“foreground”表示單元格背景的頂層。

用法:

void Format::setFgColor( $color )

參數:此函數接受單個參數$color,該參數將color的值作為字符串(例如“ red”,“ green”)使用,並通過在8到63之間指定顏色代碼來接受另一種方法。


返回值:成功時返回TRUE,失敗時返回PEAR_ERROR。

範例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, "GeeksforGeeks", $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(); 
?>

輸出:

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

輸出:

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



相關用法


注:本文由純淨天空篩選整理自sarthak_ishu11大神的英文原創作品 PHP | Spreadsheet_Excel_Writer | setFgColor() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。