本文實例講述了html' target='_blank'>jquery+php實現(xiàn)導(dǎo)出datatables插件數(shù)據(jù)到excel的方法。分享給大家供大家參考。具體如下:DataTables是一個jQuery的表格插件。這是一個高度靈活的工具,依據(jù)的基礎(chǔ)逐步增強,這將增加先進(jìn)的互動控制,支持任何HTML表格。主要特點:1. 自動分頁處理 2. 即時表格數(shù)據(jù)過濾 3. 數(shù)據(jù)排序以及數(shù)據(jù)類型自動檢測 4. 自動處理列寬度 5. 可通過CSS定制樣式 6. 支持隱藏列 7. 易用 8. 可擴(kuò)展性和靈活性 9. 國際化 10.動態(tài)創(chuàng)建表格 11.免費插件地址http://www.datatables.net/不過可惜的是官方網(wǎng)站表格數(shù)據(jù)導(dǎo)出方法使用的是tabletools插件,利用flash導(dǎo)出數(shù)據(jù),而且不支持中文數(shù)據(jù),通過查找官方的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 form enctype="multipart/form-data" method="post" action="/csv.php" textarea name="csv" '+csv+' /textarea input type="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)前頁數(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后臺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;希望本文所述對大家的php程序設(shè)計有所幫助。PHP教程