jquery+php实现导出datatables插件数据到excel的方法
《:jquery+php实现导出datatables插件数据到excel的方法》要点: PHP实战本篇章节讲解jquery+php实现导出datatables插件数据到excel的办法.分享给大家供大家参考.具体如下: PHP实战DataTables是一个jQuery的表格插件.这是一个高度灵活的工具,依据的基础逐步增强,这将增加先进的互动控制,支持任何HTML表格.主要特点: PHP实战1. 自动分页处理 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程序设计有所赞助. 欢迎参与《:jquery+php实现导出datatables插件数据到excel的方法》讨论,分享您的想法,编程之家 52php.cn为您提供专业教程。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |