I am trying to change the color and font weight of a cell and it is changing the font on all cells. Am I doing something wrong?
Here is my code:
<?php
include_once("PHPExcel/PHPExcel.php");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="test.xlsx"');
$workbook = new PHPExcel();
$worksheet = $workbook->getActiveSheet();
$blueBold = array(
"font" => array(
"bold" => true,
"color" => array("rgb" => "0000ff"),
),
);
$greenNotBold = array(
"font" => array(
"bold" => false,
"color" => array("rgb" => "00ff00"),
),
);
$worksheet->getColumnDimension("A")->setAutoSize(true);
$worksheet->setCellValue("A1", "blue, bold", true)->getStyle()->applyFromArray($blueBold);
$worksheet->setCellValue("A2", "green, not bold", true)->getStyle()->applyFromArray($greenNotBold);
$excelWriter = PHPExcel_IOFactory::createWriter($workbook, 'Excel2007');
$excelWriter->save("php://output");
What is happening: Cells A1 and A2 are always formatted with which ever style is applied last. When the cells are added in this order
$worksheet->setCellValue("A1", "blue, bold", true)->getStyle()->applyFromArray($blueBold);
$worksheet->setCellValue("A2", "green, not bold", true)->getStyle()->applyFromArray($greenNotBold);
then they are green and not bold. When they are added in this order
$worksheet->setCellValue("A2", "green, not bold", true)->getStyle()->applyFromArray($greenNotBold);
$worksheet->setCellValue("A1", "blue, bold", true)->getStyle()->applyFromArray($blueBold);
they are blue and bold
What should happen:
Cell A1 should be blue and bold. Cell A2 should be green and not bold.