简体   繁体   English

如何通过使用php或js提供相应的按钮将html表导出为ex​​cel或pdf

[英]how to export a html table into excel or pdf by giving the respective buttons using php or js

I had created a table where there will be leave details of employees and applied sorting of the leave details from date to date. 我创建了一个表,其中将有员工的请假详细信息,并应用了迄今为止对请假详细信息的排序。 It will display me in table format. 它将以表格格式显示我。 Now i want to add button like Export to Excel and Export to pdf so that i can download the table from there in excel or pdf format. 现在,我想添加“导出到Excel”和“导出到pdf”之类的按钮,以便我可以从那里以excel或pdf格式下载表格。

Below is code 下面是代码

<form name="filter" method="POST">

                            <input type="date" name="start">
                            <input type="date" name="end">
                            <input type="submit" name="submit" value="Filter">
                            </form>

   <table id="example" class="display" style="width:100%">
<tbody>
<?php
  if(isset($_POST['submit'])){
    $Status=1;
    $end = $_POST['end'];
    $start = $_POST['start'];
    $sql = "SELECT * FROM tblleaves 
       WHERE Status='".$Status."' AND (start BETWEEN '".$start."' AND '".$end."' OR end BETWEEN '".$start."' AND '".$end."' + INTERVAL 1 DAY )" ; 
   $result = mysqli_query($conn,$sql)or die(mysqli_error($conn));

     echo "<table>";
     echo "<thead>
                                    <tr>

                                         <th>title</th>
                                         <th>empid</th>
                                         <th>Team</th>
                                         <th>Leave Type</th>
                                        <th>Start Date</th>
                                        <th>End Date</th>
                                         <th>Description</th>

                                    </tr>
                                </thead>";

  while($row = mysqli_fetch_array($result)) {
$id = $row['id'];
$LeaveType = $row['LeaveType'];
$end = $row['end'];
$start=$row['start'];
$Description=$row['Description'];

$Status=$row['Status'];
$empid=$row['empid'];
$team=$row['team'];
$title=$row['title'];
echo "<td style='width: 200px;'>".$title."</td><td>".$empid."</td><td>".$team."</td><td style='width: 100px;'>".$LeaveType."</td><td>".$start."</td><td>".$end."</td><td>".$Description."</td></tr>";
      } 

    echo "</table>";

}
  else{

require('../includes/config.php');

$sql = "SELECT * FROM tblleaves where Status=1 order by id desc";

$result = mysqli_query($conn,$sql)or die(mysqli_error());

 echo "<table>";
 echo "<thead>
                                    <tr>

                                        <th>title</th>
                                         <th>empid</th>
                                         <th>Team</th>
                                        <th>Leave Type</th>
                                        <th>Start Date</th>
                                        <th>End Date</th>
                                         <th>Description</th>


                                    </tr>
                                </thead>";

  while($row = mysqli_fetch_array($result)) {
$id = $row['id'];
$LeaveType = $row['LeaveType'];
$end = $row['end'];
$start=$row['start'];
$Description=$row['Description'];

$Status=$row['Status'];
$empid=$row['empid'];
$team=$row['team'];
$title=$row['title'];
echo "<td style='width: 200px;'>".$title."</td><td>".$empid."</td><td>".$team."</td><td style='width: 100px;'>".$LeaveType."</td><td>".$start."</td><td>".$end."</td><td>".$Description."</td></tr>";
} 

  echo "</table>";
  mysqli_close($conn);

    }
 ?>                     </tbody>
                            </table>

要将html表导出为pdf,可以使用dompdf库。

There are multiple PDF libraries available to convert table to pdf, like DOMPDF, FPDF, etc. Also you can find libraries to convert table to .xls 有多个PDF库可用于将表转换为pdf,例如DOMPDF,FPDF等。您还可以找到将表转换为.xls的库。

Please go through this once. 请检查一次。 Might be you find this useful. 也许您会发现这很有用。 How to export an html table as an xlsx file 如何将html表导出为xlsx文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM