这是写好的例子可以测试一下就明白了

 
  
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html xmlns="http://www.w3.org/1999/xhtml"> 
  3.  <head> 
  4.   <title> new document </title> 
  5.   <meta name="generator" content="editplus" /> 
  6.   <meta name="author" content="" /> 
  7.   <meta name="keywords" content="" /> 
  8.   <meta name="description" content="" /> 
  9.   <SCRIPT LANGUAGE="javascript">   
  10.   <!--   
  11.   function AutomateExcel()   
  12.   {   
  13.   // Start Excel and get Application object.   
  14.   var oXL = new ActiveXObject("Excel.Application");   
  15.   // Get a new workbook.   
  16.   var oWB = oXL.Workbooks.Add();   
  17.   var oSheet = oWB.ActiveSheet;   
  18.   var table = document.all.data;   
  19.   var hang = table.rows.length;  
  20.   var lie = table.rows(0).cells.length;   
  21.   // Add table headers going cell by cell.   
  22.   for (i=0;i<hang;i++)   
  23.   {   
  24.   for (j=0;j<lie;j++)   
  25.   {   
  26.   //oSheet.Cells(i+1,j+1).Font.Bold = True   
  27.   //oSheet.Cells(i+1,j+1).Font.Size = 50   
  28.   //oSheet.Cells(i+1,j+1).Alignment = 2 
  29.   oSheet.Cells(i+1,j+1).value = table.rows(i).cells(j).innerText;   
  30.   }   
  31.   }   
  32.   oXL.Visible = true;   
  33.   oXL.UserControl = true;   
  34.   }   
  35.   //--> 
  36.   </SCRIPT> 
  37.  </head> 
  38.  
  39.  
  40.  <body> 
  41.    <table border="1" width="100%" id="data"> 
  42.   <tr> 
  43.    <td>&nbsp;姓名</td> 
  44.    <td colspan="2">年龄</td> 
  45.    <td>出生日期</td> 
  46.   </tr> 
  47.   <tr> 
  48.    <td>6</td> 
  49.    <td>25</td> 
  50.    <td colspan="2">8</td> 
  51.   </tr> 
  52.   <tr> 
  53.    <td height="22">1</td> 
  54.    <td height="22">2</td> 
  55.    <td colspan="2" height="22">4</td> 
  56.   </tr> 
  57.   </table> 
  58. <input type="button" name="out_excel" onclick="AutomateExcel();" value="导出到excel" class="notPrint"> 
  59.  </body> 
  60. </html>