PHP Excel is a powerful tool that can help us export data from PHP applications to spreadsheet files. In addition to exporting data, it also provides functions such as setting the color and format of cells. In this article, we will explain how to set the background color of table cells using PHP Excel.
The first step is to install PHP Excel. If you don't have it installed yet, you can install it using Composer. Run the following command:
composer require phpoffice/phpexcel
After installation, we can create a new worksheet through the following code:
require_once '/vendor/autoload.php'; use PHPExcel_IOFactory; use PHPExcel; // 创建一个新的 PHPExcel 对象 $objPHPExcel = new PHPExcel(); // 设置文档属性 $objPHPExcel->getProperties()->setCreator("Your Name") ->setLastModifiedBy("Your Name") ->setTitle("Demo") ->setSubject("Demo") ->setDescription("Demo") ->setKeywords("Demo") ->setCategory("Demo"); // 添加一个工作表 $objPHPExcel->setActiveSheetIndex(0); $objPHPExcel->getActiveSheet() ->setTitle('Demo'); // 输出 Excel 文件 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->save('demo.xlsx');
In this code, we use the PHPExcel object to create a new worksheet , this worksheet is named "Demo" and is saved in a file named demo.xlsx.
Next, we will use PHPExcel to set the background color of table cells. Use the following code to set the background color of a table cell:
// 设置单元格的颜色 $objPHPExcel->getActiveSheet()->getStyle('A1') ->getFill() ->setFillType(\PHPExcel_Style_Fill::FILL_SOLID) ->getStartColor() ->setRGB('FF0000');
In this code, we use the getStyle() method to get the style of cell A1. Then we use the getFill() method to get the fill style of the cell, the setFillType() method to set the fill type to FILL_SOLID, getStartColor() to get the fill color, and the setRGB() method to set it to red.
Now let’s modify the previous code and add the code to set the background color of cell A1:
require_once '/vendor/autoload.php'; use PHPExcel_IOFactory; use PHPExcel; // 创建一个新的 PHPExcel 对象 $objPHPExcel = new PHPExcel(); // 设置文档属性 $objPHPExcel->getProperties()->setCreator("Your Name") ->setLastModifiedBy("Your Name") ->setTitle("Demo") ->setSubject("Demo") ->setDescription("Demo") ->setKeywords("Demo") ->setCategory("Demo"); // 添加一个工作表 $objPHPExcel->setActiveSheetIndex(0); $objPHPExcel->getActiveSheet() ->setTitle('Demo'); // 设置单元格的颜色 $objPHPExcel->getActiveSheet()->getStyle('A1') ->getFill() ->setFillType(\PHPExcel_Style_Fill::FILL_SOLID) ->getStartColor() ->setRGB('FF0000'); // 输出 Excel 文件 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->save('demo.xlsx');
After executing the above code, run the generated Excel file and you can see the cell The background color of cell A1 has been set to red.
Summary
In this article, we learned how to set the background color of table cells using PHPExcel. By using the getStyle() and getFill() methods as well as the setFillType() and setRGB() methods, we can easily modify the color and style of the cells to make our data more attractive and readable. Hope this article can be helpful to you.
The above is the detailed content of How to set the background color of table cells using PHP Excel. For more information, please follow other related articles on the PHP Chinese website!