datatables是一个jquery的表格插件。这是一个高度灵活的工具,依据的基础逐步增强,这将增加先进的互动控制,支持任何html表格。主要特点:
不过可惜的是官方网站表格数据导出方法使用的是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 p is already open, delete it
if($('.csv-data').length) $('.csv-data').remove();
// open a p with a download link
$('body').append('<p 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></p>');
}
function strip_tags(html) {
var tmp = document.createElement("p");
tmp.innerHTML = html;
return tmp.textContent||tmp.innerText;
}函数支持导出所有数据和当前页数据
智慧车行小程序,是一个专门为洗车/4S/车辆维修行业打造的小程序,前后端完整代码包括车行动态,养车常识,保养预约,维修预约,洗车美容预约,汽车检测预约等功能。采用腾讯提供的小程序云开发解决方案,无须服务器和域名预约管理:开始/截止时间/人数均可灵活设置,可以自定义客户预约填写的数据项预约凭证:支持线下到场后校验签到/核销/二维码自助签到等多种方式详尽的预约数据:支持预约名单数据导出Excel,打印
0
// 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;
全网最新最细最实用WPS零基础入门到精通全套教程!带你真正掌握WPS办公! 内含Excel基础操作、函数设计、数据透视表等
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号