以下代码可以在IE上使用,需要允许执行new ActiveXObject ,在IE浏览器里设置允许执行,IE版本可能会有所限制,大家感兴趣的可以尝试一下!
<span style="font-size:18px;"><span style="font-size:18px;"><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" language="javascript">
var idTmr;
function method1(tableid) {//整个表格拷贝到EXCEL中
var curTbl = document.getElementById(tableid);
//var curBtn= document.getElementById(tableid);
var oXL = new ActiveXObject("Excel.Application");
//创建AX对象excel
var oWB = oXL.Workbooks.Add();
//获取workbook对象
var xlsheet = oWB.Worksheets(1);
//激活当前sheet
var sel = document.body.createTextRange();
sel.moveToElementText(curTbl);
//把表格中的内容移到TextRange中
sel.select();
//全选TextRange中内容
sel.execCommand("Copy");
//复制TextRange中内容
xlsheet.Paste();
//粘贴到活动的EXCEL中
oXL.Visible = true;
//设置excel可见属性
try {
var fname = oXL.Application.GetSaveAsFilename("将Table导出到Excel.xlsx", "Excel Spreadsheets (*.xlsx), *.xlsx");
} catch (e) {
print("Nested catch caught " + e);
} finally {
oWB.SaveAs(fname);
oWB.Close(savechanges = false);
//xls.visible = false;
oXL.Quit();
oXL = null;
//结束excel进程,退出完成
//window.setInterval("Cleanup();",1);
idTmr = window.setInterval("Cleanup();", 1);
}
}
function Cleanup() {
window.clearInterval(idTmr);
CollectGarbage();
}
</script>
</head>
<body>
<table id="ta1" border=1>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>6</td>
<td><table border=1>
<tr style="background:red">
<td>a</td>
<td>b</td>
<td>c</td>
<td>d</td>
<td>e</td>
</tr>
<tr>
<td>f</td>
<td>g</td>
<td>h</td>
<td>i</td>
<td>j</td>
</tr>
</table></td>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
</table>
<table id ="ta" style="margin:100px auto" width="650" cellpadding="5" cellspacing="0" border="1">
<tr>
<td width="121" height="30" valign="middle">用户名称:</td>
<td width="663" height="30" valign="middle"><input type="text" size="20" /></td>
</tr>
<tr>
<td height="30" valign="middle">个人密码:</td>
<td height="30" valign="middle"><input type="text" size="20" /></td>
</tr>
<tr>
<td height="30" valign="middle">靓照上传:</td>
<td height="30" valign="middle"><input type="file" /></td>
</tr>
<tr>
<td height="30" valign="middle">性 别:</td>
<td height="30" valign="middle"><input type="radio" name="sex" id="man" checked="checked" /><label for="man">男</label>
<input type="radio" name="sex" id="women" /><label for="women">女</label></td>
</tr>
<tr>
<td height="30" valign="middle">生 日:</td>
<td height="30" valign="middle"><input type="text" size="5" />
年
<select>
<option selected="selected">选择</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
</select>
月
<input type="text" size="5"/>
日</td>
</tr>
<tr>
<td height="30" valign="middle">爱 好:</td>
<td height="30" valign="middle"><input type="checkbox" id="Basketball" /><label for="Basketball">篮球</label>
<input type="checkbox" id="Badminton" /><label for="Badminton">羽毛球</label>
<input type="checkbox" id="tennis" /><label for="tennis">乒乓球</label> </td>
</tr>
</table>
<input id="Button1" type="button" value="导出EXCEL"
οnclick="javascript:method1('ta')" />
</body>
</html></span></span>