How to Set Background cell color in PHPExcel

How to Set Background cell color in PHPExcel

With PHPExcel, you can create Excel files programmatically, manipulate existing files, and add various formatting options to make your data more readable and visually appealing. One of the formatting options that PHPExcel offers is setting the background color of cells. In this tutorial, you will learn how to set the background color of cells in PHPExcel.

How to Set Background cell color in PHPExcel

By following these steps, you will create visually appealing spreadsheets with custom cell colors to highlight important data:

  • Step 1: Create a new PHPExcel object
  • Step 2: Create a worksheet
  • Step 3: Set the background color of a cell
  • Step 4: Save the worksheet

Step 1: Create a new PHPExcel object

Before you can set the background color of cells, you need to create a new PHPExcel object. To do this, you can use the following code:

require_once 'path/to/PHPExcel.php';

$objPHPExcel = new PHPExcel();

This code includes the PHPExcel library and creates a new PHPExcel object.

Step 2: Create a worksheet

Next, you need to create a worksheet within the PHPExcel object. To create a new worksheet, you can use the following code:

$worksheet = $objPHPExcel->getActiveSheet();

This code creates a new worksheet and assigns it to the $worksheet variable.

Step 3: Set the background color of a cell

Now that you have a worksheet, you can set the background color of a cell. To set the background color of a cell, you need to create a new PHPExcel_Style_Fill object and set its properties. Here’s an example:

$fill = new PHPExcel_Style_Fill();
$fill->setFillType(PHPExcel_Style_Fill::FILL_SOLID)
     ->getStartColor()->setRGB('FF0000');

This code creates a new PHPExcel_Style_Fill object, sets its fill type to FILL_SOLID (which means a solid color fill), and sets its start color to red.

Once you have created the $fill object, you can apply it to a cell by setting the cell’s getStyle() property and then setting the getFill() property of the resulting PHPExcel_Style object to $fill. Here’s an example:

$cell = 'A1';
$worksheet->setCellValue($cell, 'Hello World!');
$worksheet->getStyle($cell)->getFill()->applyFromArray(array('fill' => $fill));

This code sets the value of cell A1 to “Hello World!” and applies the background color fill to the cell.

Step 4: Save the worksheet

Finally, you need to save the worksheet to a file. To do this, you can use the following code:

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('example.xlsx');

This code creates a new PHPExcel_IOFactory object, sets its file type to Excel2007, and saves the PHPExcel object to a file named “example.xlsx”.

Conclusion

In this article, we have shown you how to set the background color of cells in PHPExcel. By following these steps, you can create visually appealing spreadsheets with custom cell colors to highlight important data. With PHPExcel’s powerful functionality, you can create complex spreadsheets with ease and manipulate them programmatically to suit your needs.

Recommended Tutorials

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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