douxuanyi2813 2019-02-18 05:35
浏览 53
已采纳

如何从php导出这个数据表到Excel文件

can anyone here help with the code to export to an Excel file i hope that someone can because I already have an export button.

I want to keep the most with this code because this code works well. and if you want to see the export.php ask for it.

here a picture 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>

here is my export.php code

<?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条回答 默认 最新

  • douxi1968 2019-02-18 06:14
    关注

    I suggest you to use javascript library, it work for me Install excell javascript and FileSaver for saving file Adding 2 library

    <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>
    

    The 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条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 宝塔面板一键迁移使用不了
  • ¥15 求一个按键录像存储到内存卡的ESP32CAM代码
  • ¥15 如何单独修改下列canvas推箱子代码target参数?,插入图片代替其形状,就是哪个绿色的圆圈每关用插入的图片替代
  • ¥20 四叉树的创建和输出问题
  • ¥15 javaweb连接数据库,jsp文件加载不出来
  • ¥15 matlab关于高斯赛德尔迭代的应用编撰。(相关搜索:matlab代码|迭代法)
  • ¥15 损失匹配问题,求解答
  • ¥15 3500常用汉字书法体检测数据集下载
  • ¥15 odoo17在制造模块或采购模块良品与次品如何分流和在质检模块下如何开发
  • ¥15 Qt音乐播放器的音乐文件相对路径怎么写