phpphpexcel

Append lines into one cell in a while loop in phpexcel


I have an Query who gives me an output of 4 recordsets like:

|1  |Tree       |
|2  |Apple      |
|3  |Orange     |
|4  |Strawberry |

In my PHP file I get these output correctly. Now I want to get all these 4 recordsets after an export in one Excel cell like in A8, but there I get only the last recordset

|4  |Strawberry |

I don't know why it doesn't work. Here my code:

$MRabfrage = "SELECT motif_regularisation 
                    FROM regul_stock_devise
                    WHERE regul_stock_devise.date_regularisation = '$dreg'";
                
$MRergebnis = mysql_query($MRabfrage) or die("Query failed with error: ".mysql_error());
    
while($rowM = mysql_fetch_array($MRergebnis))
    {
        $motif        =   $rowM['motif_regularisation'];

        $objWorksheet->setCellValue('A8', $motif."\n");
        $objWorksheet->getStyle('A8')->getAlignment()->setWrapText(true);
        $rowM++;
    }

What to do to resolve these problem?


Solution

  • Do you want to get this?

    +--------+-----------+
    |4       |Tree       |
    |        |Apple      |
    |        |Orange     |
    |        |Strawberry |
    +--------+-----------+
    

    You need to append value, not overwrite it, in each iteration:

    $prev_value = $objWorksheet->getCell('A8')->getValue();
    $objWorksheet->setCellValue('A8', $prev_value.$motif."\n");