PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, October 5, 2022

[FIXED] How to set color for cell phpexcel into foreach loop

 October 05, 2022     php, phpexcel     No comments   

Issue

Now I want set text color for cell in phpexcel into foreach loop. The foreach loop is something like :

$redBold = array(
            "font" => array(
                "bold" => true,
                "color" => array("rgb" => "FF0000"),
            ),
        );
$row = 5;
$count = 0
foreach ($data as $key => $value) {
    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($count++, $row, $value['type']?$value['type']:0);
    if ($value['type'] == 1) {
        $objPHPExcel->getActiveSheet()->getStyle($count . $row)->applyFromArray($redBold);
    }
}

this code is not understand getStyle($count . $row ) because $count . $row should be A6... Is there way to set text in this case? Please help!


Solution

What do you get when you concatenate $count . $row?

getStyle() expects either a cell reference (e.g. A1, C3, IV256) or a cell range (e.g. A1:C3, B2:D4, A2:IV256 etc.

You're simply concatenating two numbers, e.g. 0 and 5 to give 05 which is meaningless in terms of cell references/ranges

You need to convert $count (which you're using as a column index, to an actual column address before concatenating

$objPHPExcel->getActiveSheet()->getStyle(PHPExcel_Cell::stringFromColumnIndex($count) . $row)->applyFromArray($redBold);

Note also that you're incrementing the column using the post-increment operator before trying to set the style for the cell, so it probably isn't going to give you the cell reference that you want



Answered By - Mark Baker
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing