js中代码:
function exportToExcel(tableid)
{
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;
}
html中调用该函数就行,要注意的时参数的传递,
比如html中有一个table ,如果的他id是table1,函数调用方式是οnclick="exportToExcel('table1')"
下面是一个html实例:
excelTest.htmlhtml 表格导出道Excel | ||||
列标题1 | 列标题2 | 类标题3 | 列标题4 | 列标题5 |
aaa | bbb | ccc | ddd | eee |
AAA | BBB | CCC | DDD | EEE |
FFF | GGG | HHH | III | JJJ |