<!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>无标题文档</title>
<style type="text/css">
<!--
.STYLE5 {font-size: 16px; color: #990000; font-weight: bold; }
-->
</style>
</head>
<script language="javascript">
//两种方法都可以实现。第一种用剪切板
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="导出为Excel" />
</label>
</body>
</html>