js 调用php 导出表格,[拿来主义]使用layui纯JS导出html页面中的table为excel
使用場(chǎng)景:
將當(dāng)前html頁面中table的數(shù)據(jù)導(dǎo)出為excel(分析:頁面無分頁,不用再查數(shù)據(jù)庫(kù),直接使用js插件導(dǎo)出數(shù)據(jù)即可)
js代碼:
layui.config({
base:?'{$Think.PLUGINS_SITE_ROOT}/layuiv2.4.3/lay/extend/'
}).extend({
excel:?'excel'
});
layui.use(['table',?'excel'],?function()?{
var?excel?=?layui.excel;
//監(jiān)聽頭工具欄事件
$('.myexport').off('click').on('click',?function(){
exportFile('data_list');
});
//表格導(dǎo)出
function?exportFile(id)?{
var?titles?=?new?Array();
//根據(jù)傳入tableID獲取表頭
var?tds?=?$("#"?+?id?+?"?thead?tr?th");
for?(var?i?=?0;?i?
titles[i]?=?$(tds[i]).text();
}
//根據(jù)傳入tableID獲取table內(nèi)容
var?bodysArr?=?new?Array();
var?trs??????=?$("#"?+?id?+?"?tbody?tr");
for?(var?j?=?0;?j?
var?contents?=?new?Array();
var?this_????=?$(trs[j]).html();
this_?=?this_.replace(/()/g,?'');
this_?=?this_.replace(/()/g,?'');
this_?=?this_.replace(/\/g,?'');
this_?=?this_.replace(/\s+/g,?'');
this_?=?this_.replace(/\r\n*/g,?'');
this_?=?this_.replace(/()/g,?'');
//console.log(this_);return?;
var?tds??????=?$(this_?+?'?td');
for?(var?i?=?0;?i?
contents[i]?=?$(tds[i]).text();
}
bodysArr.push(contents)
}
//將標(biāo)題行置頂添加到數(shù)組
bodysArr.unshift(titles);
var?type_?=?$('input[name="type"]').val();
var?cc????=?'';
if(type_?==?1){
cc?=?$('#day').val();
}else{
cc?=?$('#month').val();
}
//導(dǎo)出excel
excel.exportExcel({
sheet1:?bodysArr
},?'會(huì)員匯總-總體數(shù)據(jù)'?+?cc?+?'.xlsx',?'xlsx');
}
});
效果:
作者:OK兄 瀏覽次數(shù):478
總結(jié)
以上是生活随笔為你收集整理的js 调用php 导出表格,[拿来主义]使用layui纯JS导出html页面中的table为excel的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php实现队列上传,php实现队列
- 下一篇: 随机密码php,PHP会生成随机密码