How to Set Font Color, Font Face and Font Size in PHPExcel

1. To set the font color, font face and font size in PHPExcel, you can use the setFont() method.

2. The syntax for the setFont() method is as follows:

$objPHPExcel->getActiveSheet()->getStyle(‘A1’)->getFont()->setFont(array(
‘name’ => ‘Arial’,
‘size’ => 12,
‘color’ => array(‘rgb’ => ‘FF0000’)
));

3. The first parameter of the setFont() method is the font name. You can use any font name that is available on your system.

4. The second parameter of the setFont() method is the font size. You can specify any font size that you want.

5. The third parameter of the setFont() method is the font color. You can specify any color that you want using the RGB color code.

6. Once you have set the font color, font face and font size, you can apply it to any cell in the worksheet using the setFont() method.

Set font color, face, and size using phpExcel; In this tutorial, you will learn how to set font color, font face, and font size using PHPExcel.

How to Set Font Color, Font Face and Font Size in PHPExcel

  • Setting Font Color in PHPExcel
  • Setting Font Face in PHPExcel
  • Setting Font Size in PHPExcel
  • Combining Font Styles in PHPExcel

Setting Font Color in PHPExcel

Setting font color in PHPExcel is a breeze. All you need to do is select the cell or range of cells you want to modify, and then use the “getStyle” method to access the font object. Once you have the font object, you can use the “setColor” method to set the desired font color.

For example, to set the font color of cell A1 to red, you would use the following code:

$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setColor(new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_RED));

Here’s is another example:

// Create a new font object
$font = new PHPExcel_Style_Font();

// Set the font color to red
$font->setColor(new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_RED));

// Apply the font style to a cell
$worksheet->getStyle('A1')->getFont()->applyFromArray(array(
    'font' => $font
));

In the example above, you first create a new font object and set its color to red using the setColor() method. You then apply the font style to a cell by using the applyFromArray() method of the PHPExcel_Style_Font class.

Setting Font Face in PHPExcel

Setting font face in PHPExcel is also simple. You can use the “getStyle” method to access the font object, and then use the “setName” method to set the desired font face.

For example, to set the font face of cell A1 to Arial, you would use the following code:

$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setName('Arial');

Here is another example:

// Create a new font object
$font = new PHPExcel_Style_Font();

// Set the font face to Arial
$font->setName('Arial');

// Apply the font style to a cell
$worksheet->getStyle('A1')->getFont()->applyFromArray(array(
    'font' => $font
));

In the example above, you create a new font object and set its name to Arial using the setName() method. You then apply the font style to a cell using the applyFromArray() method.

Setting Font Size in PHPExcel

Setting font size in PHPExcel is just as straightforward. You can use the “getStyle” method to access the font object, and then use the “setSize” method to set the desired font size.

For example, to set the font size of cell A1 to 14, you would use the following code:

$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(14);

Here is another example:

// Create a new font object
$font = new PHPExcel_Style_Font();

// Set the font size to 12
$font->setSize(12);

// Apply the font style to a cell
$worksheet->getStyle('A1')->getFont()->applyFromArray(array(
    'font' => $font
));

In the example above, you create a new font object and set its size to 12 using the setSize() method. You then apply the font style to a cell using the applyFromArray() method.

Combining Font Styles in PHPExcel

Of course, you can also combine these font styles to achieve more complex formatting. For example, to set the font color, font face, and font size of cell A1, you would use the following code:

$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()
    ->setColor(new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_RED))
    ->setName('Arial')
    ->setSize(14);

Here is an another example:

// Create a new font object
$font = new PHPExcel_Style_Font();

// Set the font properties
$font->setColor(new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_BLUE));
$font->setName('Times New Roman');
$font->setSize(16);

// Apply the font style to a cell
$worksheet->getStyle('A1')->getFont()->applyFromArray(array(
    'font' => $font
));

In the example above, you create a new font object and set its color to blue, name to Times New Roman, and size to 16. You then apply the font style to a cell using the applyFromArray() method.

Conclusion

With PHPExcel, it’s easy to set font color, font face, and font size, allowing you to customize the appearance of your spreadsheets to suit your needs. By using the “getStyle” method to access the font object, you can quickly modify the formatting of your cells to create professional-looking documents that are both functional and aesthetically pleasing.

Recommended Tutorials

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?