Using phpxcel cell getstyle applyfromarray

I am trying to change the font color and weight of a cell and it changes 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's Happening: Cells A1 and A2 are always formatted with the style ever applied. When 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 greasy. 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 is going to happen:

Cell A1 should be blue and bold. Cell A2 should be green and not bold.

+3


source to share


1 answer


You clearly found the bug somewhere.

In the meantime (while I cannot determine the cause and apply the fix), I would recommend doing

$worksheet->setCellValue("A1", "blue, bold")
    ->getStyle('A1')->applyFromArray($blueBold);
$worksheet->setCellValue("A2", "green, not bold")
    ->getStyle('A2')->applyFromArray($greenNotBold);

      



instead

EDIT

The fix for this issue is now applied to the development branch on github

+2


source







All Articles