您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

jquery导出数据到excel代码实例详解

2024/3/29 9:24:29发布5次查看
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; }
函数支持导出所有数据和当前页数据
// 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;
以上就是jquery导出数据到excel代码实例详解的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录