溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

怎么在php中利用jquery將datatables數(shù)據(jù)倒到excel

發(fā)布時(shí)間:2021-01-29 16:17:41 來(lái)源:億速云 閱讀:189 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

本篇文章為大家展示了怎么在php中利用jquery將datatables數(shù)據(jù)倒到excel,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

DataTables是一個(gè)jQuery的表格插件。這是一個(gè)高度靈活的工具,依據(jù)的基礎(chǔ)逐步增強(qiáng),這將增加先進(jìn)的互動(dòng)控制,支持任何HTML表格。主要特點(diǎn):

1. 自動(dòng)分頁(yè)處理
2. 即時(shí)表格數(shù)據(jù)過(guò)濾
3. 數(shù)據(jù)排序以及數(shù)據(jù)類(lèi)型自動(dòng)檢測(cè)
4. 自動(dòng)處理列寬度
5. 可通過(guò)CSS定制樣式
6. 支持隱藏列
7. 易用
8. 可擴(kuò)展性和靈活性
9. 國(guó)際化
10.動(dòng)態(tài)創(chuàng)建表格
11.免費(fèi)

插件地址http://www.datatables.net/

不過(guò)可惜的是官方網(wǎng)站表格數(shù)據(jù)導(dǎo)出方法使用的是tabletools插件,利用flash導(dǎo)出數(shù)據(jù),而且不支持中文數(shù)據(jù),通過(guò)查找官方的API和資料,找到使用jquery和php導(dǎo)出數(shù)據(jù)方法。

導(dǎo)出數(shù)據(jù)的javascript函數(shù)

function table2csv(oTable, exportmode, tableElm) { 
    var csv = ''; 
    var headers = []; 
    var rows = []; 
    // Get header names 
    $(tableElm+' thead').find('th').each(function() { 
      var $th = $(this); 
      var text = $th.text(); 
      var header = '"' + text + '"'; 
      // headers.push(header); // original code 
      if(text != "") headers.push(header); // actually datatables seems to copy my original headers so there ist an amount of TH cells which are empty 
    }); 
    csv += headers.join(',') + "\n"; 
    // get table data 
    if (exportmode == "full") { // total data 
      var total = oTable.fnSettings().fnRecordsTotal() 
      for(i = 0; i < total; i++) { 
        var row = oTable.fnGetData(i); 
        row = strip_tags(row); 
        rows.push(row); 
      } 
    } else { // visible rows only 
      $(tableElm+' tbody tr:visible').each(function(index) { 
        var row = oTable.fnGetData(this); 
        row = strip_tags(row); 
        rows.push(row); 
      }) 
    } 
    csv += rows.join("\n"); 
    // if a csv div is already open, delete it 
    if($('.csv-data').length) $('.csv-data').remove(); 
    // open a div with a download link 
    $('body').append('<div class="csv-data"><form enctype="multipart/form-data" method="post" action="/csv.php"><textarea class="form" name="csv">'+csv+'</textarea><input type="submit" class="submit" value="Download as file" /></form></div>'); 
} 
function strip_tags(html) { 
  var tmp = document.createElement("div"); 
  tmp.innerHTML = html; 
  return tmp.textContent||tmp.innerText; 
}

函數(shù)支持導(dǎo)出所有數(shù)據(jù)和當(dāng)前頁(yè)數(shù)據(jù)

// export only what is visible right now (filters & paginationapplied)
$('#export_visible').click(function(event) {  
   var oTable; 
   oTable= $('#spdata').dataTable(); 
   event.preventDefault(); 
   table2csv(oTable, 'visible', '#spdata'); })
   // export all table data 
$('#export_all').click(function(event) {  
  var oTable; 
  oTable= $('#spdata').dataTable(); 
  event.preventDefault(); 
 table2csv(oTable, 'full', '#spdata'); })

其中#spdata是table的id 

后臺(tái)php導(dǎo)出excel代碼

header("Content-Type: application/vnd.ms-execl");  
header("Content-Disposition: attachment; filename=myExcel.csv");  
header("Pragma: no-cache");  
header("Expires: 0");  
$buffer = $_POST['csv'];     
$buffer=str_replace(",",",\t",$buffer); 
$buffer=mb_convert_encoding($buffer,"GB2312","UTF-8");  
echo $buffer;

上述內(nèi)容就是怎么在php中利用jquery將datatables數(shù)據(jù)倒到excel,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI