<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>無標(biāo)題文檔</title>
<style type="text/css">
<!--
.STYLE5 {font-size: 16px; color: #990000; font-weight: bold; }
-->
</style>
</head>
<script language="javascript">
//兩種方法都可以實(shí)現(xiàn)。第一種用剪切板
function exports() {
oApplication = new ActiveXObject ( 'Excel.Application' );
oApplication.DisplayAlerts = false;
var xlBook = oApplication.Workbooks.Add;
oActiveSheet = xlBook.Worksheets(1);
window.clipboardData.setData('Text',document.getElementById("table1").outerHTML);
alert(document.getElementById("table1").rows.length);
oActiveSheet.Paste;
oApplication.Visible = true;
oApplication.UserControl = true;
}
//第二種逐單元格處理。
function exports2()
{
oApplication = new ActiveXObject ( 'Excel.Application' );
oApplication.DisplayAlerts = false;
var xlBook = oApplication.Workbooks.Add;
oActiveSheet = xlBook.Worksheets(1);
var rowLength = document.getElementById("table1").rows.length;
var colLength = document.getElementById("table1").rows[0].cells.length;
for (i=0;i< rowLength;i++ )
{
for (j=0;j< colLength ;j++ )
{
oActiveSheet.Cells(i+1,j+1).formulaR1C1= document.getElementById("table1").rows[ i ].cells[ j ].innerText;
}
}
oApplication.Visible = true;
oApplication.UserControl = true;
}
</script>
<body>
<table width="1100" border="1" id="table1">
<tr>
<td bgcolor="#99CC99"><span class="STYLE5">bbbbbb</span></td>
<td bgcolor="#99CC99"><span class="STYLE5">男</span></td>
<td bgcolor="#99CC99"><span class="STYLE5">漢族</span></td>
<td bgcolor="#99CC99"><span class="STYLE5">河南</span></td>
</tr>
<tr>
<td bgcolor="#99CC99"><span class="STYLE5">aaaaa</span></td>
<td bgcolor="#99CC99"><span class="STYLE5">男</span></td>
<td bgcolor="#99CC99"><span class="STYLE5">漢族</span></td>
<td bgcolor="#99CC99"><span class="STYLE5">河南</span></td>
</tr>
<tr>
<td bgcolor="#99CC99"><span class="STYLE5">aaaa</span></td>
<td bgcolor="#99CC99"><span class="STYLE5">男</span></td>
<td bgcolor="#99CC99"><span class="STYLE5">漢族</span></td>
<td bgcolor="#99CC99"><span class="STYLE5">河南</span></td>
</tr>
<tr>
<td bgcolor="#99CC99"><span class="STYLE5">aaaaa</span></td>
<td bgcolor="#99CC99"><span class="STYLE5">男</span></td>
<td bgcolor="#99CC99"><span class="STYLE5">漢族</span></td>
<td bgcolor="#99CC99"><span class="STYLE5">河南</span></td>
</tr>
<tr>
<td bgcolor="#99CC99"><span class="STYLE5">bbbbb</span></td>
<td bgcolor="#99CC99"><span class="STYLE5">男</span></td>
<td bgcolor="#99CC99"><span class="STYLE5">漢族</span></td>
<td bgcolor="#99CC99"><span class="STYLE5">河南</span></td>
</tr>
</table>
<label>
<input name="button" type="button" id="button" onclick="exports();" value="導(dǎo)出為Excel" />
</label>
</body>
</html>