《PHP學習:jquery+php實現導出datatables插件數據到excel的方法》要點:
本文介紹了PHP學習:jquery+php實現導出datatables插件數據到excel的方法,希望對您有用。如果有疑問,可以聯系我們。
PHP實戰本文實例講述了jquery+php實現導出datatables插件數據到excel的辦法.分享給大家供大家參考.具體如下:
PHP實戰DataTables是一個jQuery的表格插件.這是一個高度靈活的工具,依據的基礎逐步增強,這將增加先進的互動控制,支持任何HTML表格.主要特點:
PHP實戰1. 自動分頁處理
2. 即時表格數據過濾
3. 數據排序以及數據類型自動檢測
4. 自動處理列寬度
5. 可通過CSS定制樣式
6. 支持暗藏列
7. 易用
8. 可擴展性和靈活性
9. 國際化
10.動態創建表格
11.免費
PHP實戰插件地址http://www.datatables.net/
PHP實戰不過可惜的是官方網站表格數據導出辦法使用的是tabletools插件,利用flash導出數據,而且不支持中文數據,通過查找官方的API和資料,找到使用jquery和php導出數據辦法.
PHP實戰導出數據的javascript函數
PHP實戰
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;
}
PHP實戰函數支持導出所有數據和當前頁數據
PHP實戰
// 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'); })
PHP實戰其中#spdata是table的id?
PHP實戰后臺php導出excel代碼
PHP實戰
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實戰希望本文所述對大家的php程序設計有所贊助.
歡迎參與《PHP學習:jquery+php實現導出datatables插件數據到excel的方法》討論,分享您的想法,維易PHP學院為您提供專業教程。
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/9741.html