douchen5971 2014-09-09 21:53
浏览 107
已采纳

phpexcel cell getstyle applyfromarray

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.

  • 写回答

1条回答 默认 最新

  • dsk61780 2014-09-09 22:30
    关注

    You've clearly found a bug somewhere.

    In the meanwhile (until I can identify the cause and apply a fix), I'd recommend doing

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

    instead

    EDIT

    A fix for this issue has now been applied to the develop branch on github

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大