/p>
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
WEB页面导出为EXCEL文档的方法WEB页面导出为EXCEL文档的方法 | ||||
列标题1 | 列标题2 | 列标题3 | 列标题4 | 列标题5 |
aaa | bbb | ccc | ddd | eee |
AAA | BBB | CCC | DDD | EEE |
FFF | GGG | HHH | III | JJJ |
妞妞 | |||
列标题1 | 列标题2 | 列标题3 | 列标题5 |
AAA | BBB | CCC | EEE |
FFF | GGG | HHH | JJJ |
function exec(){
var tb=$("#tableExcel").html();
var tb1=$("#tableExcel1").html();
var tabhtml='
';var cells1=$($("#tableExcel").find("tr")[1]).find("td").length;
var cells2=$($("#tableExcel1").find("tr")[1]).find("td").length;
$(".ww").html(tabhtml);
exportExcel('tableExcel2','name','WEB页面导出为EXCEL文档的方法.xls');
}
var idTmr;
function getExplorer() {
var explorer = window.navigator.userAgent ;
//ie
if (explorer.indexOf("MSIE") >= 0|| (explorer.indexOf("Windows NT 6.1;") >= 0 && explorer.indexOf("Trident/7.0;") >= 0)) {
return 'ie';
}
//firefox
else if (explorer.indexOf("Firefox") >= 0) {
return 'Firefox';
}
//Chrome
else if(explorer.indexOf("Chrome") >= 0){
return 'Chrome';
}
//Opera
else if(explorer.indexOf("Opera") >= 0){
return 'Opera';
}
//Safari
else if(explorer.indexOf("Safari") >= 0){
return 'Safari';
}
}
function exportExcel(tableid,name,filename) {
// alert(getExplorer());
if(getExplorer()=='ie') {
var curTbl = document.getElementById(tableid);
var oXL = new ActiveXObject("Excel.Application");
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var Lenr = curTbl.rows.length;
for (i = 0; i < Lenr; i++){
var Lenc = curTbl.rows(i).cells.length;
for (j = 0; j < Lenc; j++){
oSheet.Cells(i + 1, j + 1).value = curTbl.rows(i).cells(j).innerText;
}
}
oXL.Visible = true;
}else{
tableToExcel(tableid,name,filename)
}
}
function Cleanup() {
window.clearInterval(idTmr);
CollectGarbage();
}
var tableToExcel = (function() {
var uri = 'data:application/vnd.ms-excel;base64,',
template = '
',base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) },
format = function(s, c) {
return s.replace(/{(\w+)}/g,
function(m, p) { return c[p]; })
}
return function(table, name,filename) {
if (!table.nodeType) table = document.getElementById(table)
var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
document.getElementById("dlink").href = uri + base64(format(template, ctx));
document.getElementById("dlink").download = filename;
document.getElementById("dlink").click();
}
})()