Why does excel file has been created with php, is not the same with tabel in browser?

StackOverflow https://stackoverflow.com/questions/23211436

  •  07-07-2023
  •  | 
  •  

문제

Please help, I am trying to sort out the data from mysql table into an excel file by city (kol_17). And the code as follows:

$rslt = mysql_query("SELECT DISTINCT kol_17 AS bmcity FROM temp_table ORDER BY kol_17 ASC");
while ($rowgroup = mysql_fetch_array($rslt)) {
    $bm_city = $rowgroup['bmcity'];
    echo "<br>Event City: <b>$bm_city</b>";
    echo "<table border='1'>";
    echo "<tr>";
    echo "<td bgcolor='#C0C0C0'>Order Number</td>";
    echo "<td bgcolor='#C0C0C0'>Item Code</td>";
    echo "<td bgcolor='#C0C0C0'>Deposit</td>";
    echo "<td bgcolor='#C0C0C0'>Total</td>";
    echo "<td bgcolor='#C0C0C0'>Price</td>";
    echo "<td bgcolor='#C0C0C0'>Trx MSISDN</td>";
    echo "<td bgcolor='#C0C0C0'>Communication MSISDN</td>";
    echo "<td bgcolor='#C0C0C0'>BM City</td>";
    echo "<td bgcolor='#C0C0C0'>Se Area</td>";
    echo "<td bgcolor='#C0C0C0'>Type of Sale</td>";
    echo "</tr>";

    $objPHPExcel->setActiveSheetIndex(0);

    $objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Order Number');
    $objPHPExcel->getActiveSheet()->SetCellValue('B1', 'Item Code');
    $objPHPExcel->getActiveSheet()->SetCellValue('C1', 'DuiTRI');
    $objPHPExcel->getActiveSheet()->SetCellValue('D1', 'Total');
    $objPHPExcel->getActiveSheet()->SetCellValue('E1', 'Price');
    $objPHPExcel->getActiveSheet()->SetCellValue('F1', 'Trx MSISDN');
    $objPHPExcel->getActiveSheet()->SetCellValue('G1', 'Communication MSISDN');
    $objPHPExcel->getActiveSheet()->SetCellValue('H1', 'BM City');
    $objPHPExcel->getActiveSheet()->SetCellValue('I1', 'Se Area');
    $objPHPExcel->getActiveSheet()->SetCellValue('J1', 'Type of Sale');
    $result = mysql_query("SELECT kol_0, kol_5, kol_8, kol_9, kol_10, kol_14, kol_15, kol_17, kol_18, kol_20 from temp_table WHERE kol_17 = '$bm_city' AND kol_5 like '70%'");

    $colnum=1;                  
       while ($row = mysql_fetch_array($result)){
           echo "<tr>";
           echo "<td>" . $row['kol_0'] . "</td>";
           echo "<td>" . $row['kol_5'] . "</td>";
           echo "<td>" . $row['kol_8'] . "</td>";
           echo "<td>" . $row['kol_9'] . "</td>";
           echo "<td>" . $row['kol_10'] . "</td>";
           echo "<td>" . $row['kol_14'] . "</td>";
           echo "<td>" . $row['kol_15'] . "</td>";
           echo "<td>" . $row['kol_17'] . "</td>";
           echo "<td>" . $row['kol_18'] . "</td>";
           echo "<td>" . $row['kol_20'] . "</td>";
           echo "</tr>";
           $colnum++;
           $objPHPExcel->getActiveSheet()->SetCellValue('A'."$colnum", $row["kol_0"]);
           $objPHPExcel->getActiveSheet()->SetCellValue('B'."$colnum", $row["kol_5"]);
           $objPHPExcel->getActiveSheet()->SetCellValue('C'."$colnum", $row["kol_8"]);
           $objPHPExcel->getActiveSheet()->SetCellValue('D'."$colnum", $row["kol_9"]);
           $objPHPExcel->getActiveSheet()->SetCellValue('E'."$colnum", $row["kol_10"]);
           $objPHPExcel->getActiveSheet()->SetCellValue('F'."$colnum", $row["kol_14"]);
           $objPHPExcel->getActiveSheet()->SetCellValue('G'."$colnum", $row["kol_15"]);
           $objPHPExcel->getActiveSheet()->SetCellValue('H'."$colnum", $row["kol_17"]);
           $objPHPExcel->getActiveSheet()->SetCellValue('I'."$colnum", $row["kol_18"]);
           $objPHPExcel->getActiveSheet()->SetCellValue('J'."$colnum", $row["kol_20"]);
       }
       echo "</table>";

       $objPHPExcel->getActiveSheet()->setTitle('Upload');
       $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
       $sFileName = "preorder_".$rowgroup[0].".xlsx";
       $objWriter->save('upload/'.$sFileName);
}

The result, in the excel file still contained the same city that should not be, but in the browser is correct.

Thank You.

도움이 되었습니까?

해결책

Consider your code logic.

  • Fetch first city from database
  • Fetch all events from database for that city
  • Loop through all city events
    • assume there are 8 events for city #1
    • This populates rows 2-9
  • Write file for that city
  • Fetch second city from database
  • Fetch all events from database for that city
  • Loop through all city events
    • assume there are 5 events for city #2
    • This populates rows 2-6

At this stage, what is in rows 7, 8 and 9? The PHPExcel object hasn't been told that any rows should be removed, so they're still populated with events 6, 7 and 8 for city #1

Either you need to clear down all data that you've added to the worksheet from the previous city, or you need to create a new PHPExcel object for each city

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top