您的位置:首页 > Web前端 > JavaScript

JavaScript将页面中的table数据导出到excel

2009-06-04 10:38 741 查看
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;" mce_style="color:#333333;border-collapse:collapse;">
<tr style="color:White;background-color:#507CD1;font-weight:bold;" mce_style="color:White;background-color:#507CD1;font-weight:bold;">
<th scope="col">姓名</th><th scope="col">性别</th><th scope="col">年龄</th><th scope="col">电话1</th>
</tr><tr style="background-color:#EFF3FB;" mce_style="background-color:#EFF3FB;">
<td>张三0</td><td>男</td><td>0</td><td>0755-29102930</td>
</tr><tr style="background-color:White;" mce_style="background-color:White;">
<td>张三1</td><td>男</td><td>1</td><td>0755-29102931</td>
</tr><tr style="background-color:#EFF3FB;" mce_style="background-color:#EFF3FB;">
<td>张三2</td><td>男</td><td>2</td><td>0755-29102932</td>
</tr><tr style="background-color:White;" mce_style="background-color:White;">
<td>张三3</td><td>男</td><td>3</td><td>0755-29102933</td>
</tr><tr style="background-color:#EFF3FB;" mce_style="background-color:#EFF3FB;">
<td>张三4</td><td>男</td><td>4</td><td>0755-29102934</td>
</tr><tr style="background-color:White;" mce_style="background-color:White;">
<td>张三5</td><td>男</td><td>5</td><td>0755-29102935</td>
</tr>
</table>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: