将Mysql表结果导出到Excel,CSV或PDF

大卫·穆科罗(David Mukoro)

我有一个从mysql数据库生成的报告,但我希望用户能够将其导出到excel或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格式的代码。

凤凰艺术

只需像这样制作一个单独的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/zh/wrappers.php.php

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章