当前位置: 移动技术网 > IT编程>开发语言>PHP > jquery+php实现导出datatables插件数据到excel的方法

jquery+php实现导出datatables插件数据到excel的方法

2018年05月15日  | 移动技术网IT编程  | 我要评论

本文实例讲述了jquery+php实现导出datatables插件数据到excel的方法。分享给大家供大家参考。具体如下:

datatables是一个jquery的表格插件。这是一个高度灵活的工具,依据的基础逐步增强,这将增加先进的互动控制,支持任何html表格。主要特点:

1. 自动分页处理
2. 即时表格数据过滤
3. 数据排序以及数据类型自动检测
4. 自动处理列宽度
5. 可通过css定制样式
6. 支持隐藏列
7. 易用
8. 可扩展性和灵活性
9. 国际化
10.动态创建表格
11.免费

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

不过可惜的是官方网站表格数据导出方法使用的是tabletools插件,利用flash导出数据,而且不支持中文数据,通过查找官方的api和资料,找到使用jquery和php导出数据方法。

导出数据的javascript函数

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

函数支持导出所有数据和当前页数据

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

后台php导出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;

希望本文所述对大家的php程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网