将我的php表数据导出到csv并下载 [英] Export my php table data to csv and download

查看:50
本文介绍了将我的php表数据导出到csv并下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的datbase提取的数据获取到Excel文件中,以将其下载到execl或csv中,但是我在导出时遇到了问题.没有数据正在提取到csv.这是我的代码:

I'm trying to get my datbase fetched data to Excel file to download it in execl or csv, but I'm having problems with exporting. Not datas are fetching to the csv .Here is my code:

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

推荐答案

///将此功能添加到脚本标签

//Add this function to script tag

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);
        }

这篇关于将我的php表数据导出到csv并下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆