找了N久,终于找到了一个可用的办法,但会IE提出安全提示,但还能用。记录下来
在IE7下测试的安全提示,如图:
js:
function toexcel()
{
// start excel and get application object.
var oxl = new ActiveXObject("excel.application");
// get a new workbook.
var owb = oxl.workbooks.add();
var osheet = owb.activesheet;
var table = document.all.GridView1;
var hang = table.rows.length;
var lie = table.rows(0).cells.length;
// add table headers going cell by cell.
for (i = 0; i < hang; i ++ )
{
for (j = 0; j < lie; j ++ )
{
osheet.cells(i + 1, j + 1).value = table.rows(i).cells(j).innerText;
}
}
oxl.visible = true;
oxl.usercontrol = true;
}
html:
<input type="button" name="button2" value="导出EXCEL" onclick="toexcel();" />
<table cellspacing="0" cellpadding="4" border="0" id="GridView1" style="color:#333333;border-collapse:collapse;">
<tr style="color:White;background-color:#507CD1;font-weight:bold;">
<th scope="col">姓名th><th scope="col">性别th><th scope="col">年龄th><th scope="col">电话1th>
tr><tr style="background-color:#EFF3FB;">
<td>张三0td><td>男td><td>0td><td>0755-29102930td>
tr><tr style="background-color:White;">
<td>张三1td><td>男td><td>1td><td>0755-29102931td>
tr><tr style="background-color:#EFF3FB;">
<td>张三2td><td>男td><td>2td><td>0755-29102932td>
tr><tr style="background-color:White;">
<td>张三3td><td>男td><td>3td><td>0755-29102933td>
tr><tr style="background-color:#EFF3FB;">
<td>张三4td><td>男td><td>4td><td>0755-29102934td>
tr><tr style="background-color:White;">
<td>张三5td><td>男td><td>5td><td>0755-29102935td>
tr>
table>
阅读(1535) | 评论(0) | 转发(0) |