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

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

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

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *