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

Wednesday, October 5, 2022

[FIXED] How to dynamically add to PHPExcel spreadsheet

 October 05, 2022     mysql, pdo, php, phpexcel     No comments   

Issue

Alright, So I have this code

        $key = $_SESSION['order_nums'];
        $sqll = "SELECT * FROM `money` WHERE `order` = :key";
        $qq=$con->prepare($sqll);
        $qq->bindvalue(":key", $key);
        $qq->execute();


        $excel2 = PHPExcel_IOFactory::createReader('Excel2007');
        $excel2 = $excel2->load('nTest.xlsx'); // Empty Sheet
        $excel2->setActiveSheetIndex(0);
        $worksheet = $excel2->getActiveSheet();

        while($fdata=$qq->fetch(PDO::FETCH_ASSOC))
        {   
            $worksheet
                ->setCellValue('A7', $fdata['code']);


        }

Where it it setting the cell value for A7 there is about 6 more of those that match. When I do it like this however, It only puts its into the CELL A7

            while($fdata=$qq->fetch(PDO::FETCH_ASSOC))
        {   
            $worksheet
                ->setCellValue('A7', $fdata['code']);


        }

How can I make it where The above value will Drop down one cell for each new entry.

So the next would be A8, A9..... and so on.


Solution

That 'A7' isn't some kind of magic value, it's just a normal PHP string that's passed as a standard function argument to the setCellValue() method.... you can replace it with a string variable that you define yourself, and change for each row

    $column = 'A';
    $row = 7;
    while($fdata=$qq->fetch(PDO::FETCH_ASSOC))
    {   
        $worksheet
            ->setCellValue($column . $row, $fdata['code']);
        $row++;

    }


Answered By - Mark Baker
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • 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

1,268,850

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 © 2025 PHPFixing