如何从php将此数据表导出到Excel文件

时间:2019-02-18 13:35:02

标签: php excel

这里有人可以帮助将代码导出到Excel文件吗,我希望有人可以,因为我已经有一个导出按钮。

我想尽可能多地使用此代码,因为此代码运行良好。并且如果您想看到export.php要求它。

这是一张图片 enter image description here

<a class="btn btnx btn-orange" href="export.php" target="_new"><i class="fa fa-download"></i> Export to Excel</a>
         <div class="table-responsive">                  
      <table id="mytable" class="table table-bordred table-striped">                 
         <thead>
           <tr>
           <th>#</th>
           <th>Auditeur</th>
           <th>Afdeling</th>
           <th>Invoerdatum</th>
           <th>Week</th>
           <th>Zone</th>
           <th>Stand</th>
           <th>Zijn alle overbodeige gereedschappen / materialen van de werkpost verwijderd / geïdentificeerd? ( sleutel, bouten / moeren,.... )</th>
           <th>Opgeslagen foto nr1</th>
           <th>Is er rommel aanwezig op de werkpost ( bekertjes, kledij, flesjes,....)</th>
           <th>Opgeslagen foto nr2</th>
           <th>Zijn vervallen / niet geautoriseerde documenten verwijderd?</th>
           <th>Opgeslagen foto nr3</th>
           <th>Opmerking</th>
           <th>Edit</th>
           <th>Delete</th>
           </tr>
         </thead>
       <tbody>

<?php

      $fetchdata=new DB_con(); 
      $sql=$fetchdata->fetchdata();
      $cnt=1;
         while($row=mysqli_fetch_array($sql))

     {
?> 

       <tr>       
       <td><?php echo htmlentities($cnt);?></td>
       <td><?php echo htmlentities($row['Auditeur']);?></td>
       <td><?php echo htmlentities($row['Afdeling']);?></td>
       <td><?php echo htmlentities($row['PostingDate']);?></td>
       <td><?php echo htmlentities($row['Week']);?></td>
       <td><?php echo htmlentities($row['Zone']);?></td>
       <td><?php echo htmlentities($row['Stand']);?></td>
       <td><?php echo htmlentities($row['NOKOK01']);?></td>
       <td><?php echo htmlentities($row['Results']);?></td>
       <td><?php echo htmlentities($row['NOKOK02']);?></td>
       <td><?php echo htmlentities($row['Results2']);?></td>
       <td><?php echo htmlentities($row['NOKOK03']);?></td>
       <td><?php echo htmlentities($row['Results3']);?></td>
       <td><?php echo htmlentities($row['Bericht']);?></td>


       <td><a href="updat_form_sortings.php?id=<?php echo htmlentities($row['id']);?>"><button class="btn btn-info btn-xs"><span class="glyphicon glyphicon-pencil"></span></button></a></td>

       <td><a href="app_opslag_sorting.php?del=<?php echo htmlentities($row['id']);?>"><button class="btn btn-danger btn-xs" onclick="return confirm('Wil je bestand echt verwijderen?');"><span class="glyphicon glyphicon-trash"></span></button></a></td>
    </tr>


<?php 
// for serial number increment
     $cnt++;
   } 
?>

       </tbody>      
     </table>

这是我的export.php代码

<?php  
//export.php  
$connect = mysqli_connect("localhost", "root", "", "oopscrud02");
$output = '';
if(isset($_POST["export"]))
{
 $query = "SELECT * FROM tblusers";
 $result = mysqli_query($connect, $query);
 if(mysqli_num_rows($result) > 0)
 {
  $output .= '
   <table class="table" bordered="1">  
        <tr>  
          <th>#</th>  
          <th>Afdeling</th>
          <th>Invoerdatum</th>
          <th>Week</th>
          <th>Zone</th>
          <th>Stand</th>
          <th>Zijn alle overbodeige gereedschappen / materialen van de werkpost verwijderd / geïdentificeerd? ( sleutel, bouten / moeren,.... )</th>
          <th>Opgeslagen foto nr1</th>
          <th>Is er rommel aanwezig op de werkpost ( bekertjes, kledij, flesjes,....)</th>
          <th>Opgeslagen foto nr2</th>
          <th>Zijn vervallen / niet geautoriseerde documenten verwijderd?</th>
          <th>Opgeslagen foto nr3</th>          
          <th>Opmerking</th>
        </tr>
  ';
  while($row = mysqli_fetch_array($result))
  {
   $output .= '
        <tr>  
          <td>'.$row["Auditeur"].'</td>  
          <td>'.$row["Afdeling"].'</td>
          <td>'.$row["PostingDate"].'</td>
          <td>'.$row["Week"].'</td>
          <td>'.$row["Zone"].'</td>
          <td>'.$row["Stand"].'</td>  
          <td>'.$row["NOKOK01"].'</td>
          <td>'.$row["Results"].'</td>
          <td>'.$row["NOKOK02"].'</td>
          <td>'.$row["Results2"].'</td>
          <td>'.$row["NOKOK03"].'</td>
          <td>'.$row["Results3"].'</td>
          <td>'.$row["Bericht"].'</td>
        </tr>
   ';
  }
  $output .= '</table>';
  header('Content-Type: application/xls');
  header('Content-Disposition: attachment; filename=Sorterend form.xls');
  echo $output;
 }
}
?>

2 个答案:

答案 0 :(得分:0)

我建议您使用javascript库,它对我有用 安装excell javascriptFileSaver以保存文件 添加2个库

<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.8/FileSaver.min.js"></script>

XLX Js

<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.14.1/xlsx.full.min.js"></script>

脚本

<script>
  $('#download-btn').on('click', function(){
    var wb = XLSX.utils.table_to_book(document.getElementById('my-table'),{sheet: "Sheet name"})

    var wbout = XLSX.write(wb, {bookType: 'xlsx', bookSST: true, type: 'binary'});

    function s2ab(s) {
      var buf = new ArrayBuffer(s.length);
      var view = new Uint8Array(buf);
      for (var i = 0; i < s.length; i++) {
        view[i] = s.charCodeAt(i) & 0xFF;
      }
      return buf;
    }

    saveAs(new Blob([s2ab(wbout)], {type:"application/octet-stream"}), 'test.xlsx');
  })
</script>

答案 1 :(得分:0)

也许最简单的方法是使用DataTables。 查看示例:https://datatables.net/extensions/buttons/examples/initialisation/export.html 之后,您可以简单地使用css并使其具有所需的外观。