phpexcel单元格getstyle应用程序数组


phpexcel cell getstyle applyfromarray

我正在尝试更改一个单元格的颜色和字体大小,它正在更改所有单元格的字体。我做错什么了吗?

这是我的代码:

<?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");

发生了什么:单元格A1和A2总是格式化为最后应用哪种样式。按此顺序添加单元格时,

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

那么它们是绿色的而不是粗体。当它们按此顺序添加时,

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

它们是蓝色和粗体的

应该发生什么:

单元格A1应为蓝色和粗体。单元格A2应为绿色,而不是粗体。

您显然在某个地方发现了一个bug。

与此同时(直到我能够确定原因并应用修复程序),我建议进行

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

而是

编辑

此问题的修复程序现已应用于github 上的开发分支