簡體   English   中英

將我的 php 表數據導出到 csv 並下載

[英]Export my php table data to csv and download

我正在嘗試將我的 datbase 獲取的數據導入 Excel 文件,以將其下載到 execl 或 csv 中,但我在導出時遇到了問題。 不是數據正在獲取到 csv 。這是我的代碼:

<div><a href="javascript:void(0)" id="export-to-csv">Export to csv</a></div>
 <form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" id="export-form">
                        <input type="hidden" value='' id='hidden-type' name='ExportType'/>
                      </form>
      <table id="data-table" class="table table-bordered table-striped">
        <thead>
          <tr style="background-color: cornflowerblue;">
            <th>Invoice No.</th>
            <th>Invoice Date</th>
            <th>Student Name</th>
            <th>Total Amount</th>

      </tr>
    </thead>

    <?php

    if($total_rows > 0)
    {
      foreach($all_result as $row)
      {
        echo '
          <tr>
            <td>'.$row["order_no"].'</td>
            <td>'.$row["order_date"].'</td>
            <td>'.$row["order_receiver_name"].'</td>
            <td>'.$row["order_total_after_tax"].'</td>

          </tr>
        ';
      }
    }
    ?>
  </table>
  <?php
  }
  ?>

//將此函數添加到腳本標簽

function fnExcelReport()
        {
            var tab_text="<table border='2px'><tr bgcolor='#87AFC6'>";
            var textRange; var j=0;
            tab = document.getElementById('data-table'); // id of table

            for(j = 0 ; j < tab.rows.length ; j++) 
            {     
                tab_text=tab_text+tab.rows[j].innerHTML+"</tr>";
                //tab_text=tab_text+"</tr>";
            }

            tab_text=tab_text+"</table>";
            tab_text= tab_text.replace(/<A[^>]*>|<\/A>/g, "");//remove if u want links in your table
            tab_text= tab_text.replace(/<img[^>]*>/gi,""); // remove if u want images in your table
            tab_text= tab_text.replace(/<input[^>]*>|<\/input>/gi, ""); // reomves input params

            var ua = window.navigator.userAgent;
            var msie = ua.indexOf("MSIE "); 

            if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))      // If Internet Explorer
            {
                txtArea1.document.open("txt/html","replace");
                txtArea1.document.write(tab_text);
                txtArea1.document.close();
                txtArea1.focus(); 
                sa=txtArea1.document.execCommand("SaveAs",true,"Say Thanks to Sumit.xls");
            }  
            else                 //other browser not tested on IE 11
                sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));  

            return (sa);
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM