2014-10-31 104 views
-1

我有一個從mysql數據庫生成的報告,但我希望用戶能夠將其導出到Excel或PDF文件。我需要幫助。請參見下面的代碼:將Mysql表結果導出爲Excel,CSV或PDF

<h2>Staff Register</h2> 
     <table align="justify" border="1" cellspacing="0" cellpadding="0" hspace="5px" vspace="5px"> 

     <tr> 
     <th nowrap="nowrap"><div align="justify">Reg Date</div></th> 
     <th nowrap="nowrap"><div align="justify">Staff No</div></th> 
     <th nowrap="nowrap"><div align="justify">Surname</div></th> 
     <th nowrap="nowrap"><div align="justify">Firstname </div></th> 
     <th nowrap="nowrap"><div align="justify">Othername </div></th> 
     <th nowrap="nowrap"><div align="justify">Phone No</div></th> 
     <th nowrap="nowrap"><div align="justify">Sex</div></th> 
     <th nowrap="nowrap"><div align="justify">Age </div></th> 
     <th nowrap="nowrap"><div align="justify">Home Address </div></th> 
     <th nowrap="nowrap"><div align="justify">E Mail </div></th> 
     <th nowrap="nowrap"><div align="justify">Kin Name </div></th> 
     <th nowrap="nowrap"><div align="justify">Kin Add </div></th> 
      <th nowrap="nowrap"><div align="justify">Kin Phone </div></th> 
      <th nowrap="nowrap"><div align="justify">Marital Status </div></th> 
     </tr> 

    <?php 
    // Perform Inspection 
    $confirm_select = "SELECT * FROM staff_tab WHERE (status=1) ORDER BY staffno ASC"; 

    $query=$connection->query($confirm_select); 

    while($result=mysqli_fetch_array($query)){ 
     echo "<tr>"; 
     echo "<td align='justify' nowrap='nowrap'>".$result['reg_date']."</td>"; 
     echo "<td align='justify' nowrap='nowrap'>".$result['staffno']."</td>"; 
     echo "<td align='justify' nowrap='nowrap'>".$result['surname']."</td>"; 
     echo "<td align='justify' nowrap='nowrap'>".$result['firstname']."</td>"; 
     echo "<td align='justify' nowrap='nowrap'>".$result['othername']."</td>"; 
     echo "<td align='justify' nowrap='nowrap'>".$result['mobile']."</td>"; 
     echo "<td align='justify' nowrap='nowrap'>".$result['sex']."</td>"; 
     echo "<td align='justify' nowrap='nowrap'>".$result['age']."</td>"; 
     echo "<td align='justify' nowrap='nowrap'>".$result['homeadd']."</td>"; 
     echo "<td align='justify' nowrap='nowrap'>".$result['emailadd']."</td>"; 
     echo "<td align='justify' nowrap='nowrap'>".$result['nextkin']."</td>"; 
     echo "<td align='justify' nowrap='nowrap'>".$result['kinadd']."</td>"; 
     echo "<td align='justify' nowrap='nowrap'>".$result['kinphone']."</td>"; 
     echo "<td align='justify' nowrap='nowrap'>".$result['marital_status']."</td>"; 

    } 
    //to get total row count 
    $res =$connection->query("SELECT staffno FROM staff_tab WHERE status=1"); 
    $pat= ($connection->affected_rows); 
?> 
</table> 

//導出此表到Excel,CSV或PDF格式的代碼。

+0

http://stackoverflow.com/questions/4249432/export-to-csv-via-php – AVM 2014-10-31 07:02:44

回答

0

只是做一個單獨的PHP文件這樣

header('Content-type: text/csv'); 
header('Content-Disposition: attachment; filename="filename.csv"'); 

$f = fopen('php://output', 'w'); 

$confirm_select = "SELECT * FROM staff_tab WHERE (status=1) ORDER BY staffno ASC"; 
$query=$connection->query($confirm_select); 

while($result=mysqli_fetch_array($query)){ 
    fputcsv($f, $result); 
} 

當然,你必須連接到數據庫首。並將其鏈接到一個按鈕/窗體或你有什麼,我會做一個iframe等。

僅供參考。因爲它的使用不足。

http://php.net/manual/en/wrappers.php.php

+0

@ ArtisticPhoenix,我已經做了這樣的說法,但它不工作。我在表格下添加了一個超鏈接,並用上面的代碼創建了一個新頁面:<?php require_once(「/ includes/session.php」);?> <?php require_once(「/ includes/db_connection.php」) ;'> header('Content-type:text/csv'); header('Content-Disposition:attachment; filename =「filename.csv」'); $ f = fopen('staff_reg_report.php'); $ confirm_select =「SELECT * FROM staff_tab WHERE(status = 1)ORDER BY staffno ASC」; $ query = $ connection-> query($ confirm_select); ($ result = mysqli_fetch_array($ query)){ fputcsv($ f,$ result); } ?> – 2014-10-31 08:24:45

+0

@David Mukoro抱歉,我錯過了文件模式fopen('php:// output','w'); 'php:// output'是一個流包裝器,並且這個包將直接發送到輸出。這完全繞過了對文件的需要,以及在下載後刪除所述文件的任何代碼。它也使用更少的資源。 – ArtisticPhoenix 2014-10-31 21:39:47

0

我發現這是最好的答案,從解決方案到另一個人的問題,我的問題。它完美

<?php 
    require_once("/includes/session.php"); 
    require_once("/includes/db_connection.php"); 
    require_once("/includes/functions.php"); 
    // Table Name that you want 
    // to export in csv 
    $ShowTable = "staff_tab"; 
    $today=date("dmY"); 
    $FileName = "StaffRecord".$today.". csv"; 
    $file = fopen($FileName,"w"); 

    $sql = mysqli_query($connection,("SELECT * FROM $ShowTable LIMIT 500")); 
    $row = mysqli_fetch_assoc($sql); 
    // Save headings alon 
    $HeadingsArray=array(); 
    foreach($row as $name => $value){ 
    $HeadingsArray[]=$name; 
    } 
    fputcsv($file,$HeadingsArray); 
    // Save all records without headings 

    while($row = mysqli_fetch_assoc($sql)){ 
    $valuesArray=array(); 
    foreach($row as $name => $value){ 
    $valuesArray[]=$value; 
    } 
    fputcsv($file,$valuesArray); 
    } 
    fclose($file); 

    header("Location: $FileName"); 

    echo "Complete Record saves as CSV in file: <b style=\"color:red;\">$FileName</b>"; 
    ?>