AcitveXObject 來調用 Client Site 的 Excel Application 組件導出 Excel (part1)

注意:
1. 两个函数中的“data“是网页中要导出的table的 id
2. 客户端IE的安全设置中是否禁用了Activex控件下载,请设置为启用


以頁面表格方式导出到Excel代码

<html>
<body>
<table id="data" width="100%" height="25%" border="1" cellpadding="1" cellspacing="1">
<tr>
<td width="50%" align="center" bgcolor="#FFFFCC">
<strong>Title</strong></td>
<td width="50%" align="center" bgcolor="#FFFFCC">
<strong>Name</strong></td>
</tr>
<tr>
<td width="50%" align="center">
My value </td>
<td width="50%" align="center">
My value </td>
</tr>
</table>
<input type="button" name="out_excel" onclick="AutomateExcel()" value="导出到excel">
</body>
</html>

<script language="javascript">
<!--
function AutomateExcel()
{

//以頁面表格方式导出到Excel代码
var oXL = new ActiveXObject("Excel.application");
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var table = document.all.data;
var hang = table.rows.length;
var lie = table.rows(0).cells.length;
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;

}
//-->
</script>

From to here : http://topic.csdn.net/t/20040107/17/2642890.html

沒有留言: