123<script type= "Text/javascript" language= "JavaScript" >4 varIDTMR;5 functionMethod1 (TableID) {//Copy the entire table to Excel6 varCURTBL =document.getElementById (tableid);7 varOXL =NewActiveXObject ("Excel.Application");8 //Create an Ax object Excel9 varOWB =oXL.Workbooks.Add ();Ten //Get Workbook Object One varXlsheet = owb.worksheets (1); A //activating the current sheet - varSEL =Document.body.createTextRange (); - Sel.movetoelementtext (CURTBL); the //Move the contents of the table into the TextRange - Sel.select (); - //Select all the contents of TextRange -Sel.execcommand ("Copy"); + //copy content in TextRange - Xlsheet. Paste (); + //paste into active Excel AoXL.Visible =true; at //set Excel Visible Properties - - Try { - varfname = OXL.Application.GetSaveAsFilename ("Export table to Excel.xls", "Excel spreadsheets (*.xls), *.xls"); -}Catch(e) { -Print ("Nested catch caught" +e); in}finally { - Owb.saveas (fname); to +Owb.close (SaveChanges =false); - //xls.visible = false; the oxl.quit (); *OXL =NULL; $ //End Excel process, exit completePanax Notoginseng //Window.setinterval ("Cleanup ();", 1); -IDTMR = Window.setinterval ("Cleanup ();", 1); the + } A } the functionCleanup () { + Window.clearinterval (IDTMR); - collectgarbage (); $ } $</script> - - the<body> -<table id= "Ta" >Wuyi<tr> the<td>1</td><td>admin</td> -<td>23</td><td> Programmers </td> Wu<td> Tianjin </td><td>[email protected]</td> -</tr> About<tr> $<td>2</td><td>guest</td> -<td>23</td><td> Testers </td> -<td> Beijing </td><td>[email protected]</td> -</tr> A</table> +<input id= "Button1" type= "button" value= "Export Excel" theonclick= "javascript:method1 (' ta ')"/> -</body> $Or
functionDatatoexcel (TableID) {//Copy the entire table to ExcelvarCURTBL =document.getElementById (tableid);varOXL;Try{OXL= GetObject ("", "Excel.Application"); } Catch(E) {Try{OXL=NewActiveXObject ("Excel.Application"); } Catch(E2) {//alert ("Please confirm:\n1. Microsoft Excel has been installed.\n2. InternetOptions=>security=>setting \ "Enable unsafe activex\" "); Alert ("Please confirm: \n1. Excel is already installed on the machine. \n2. Internet options = = Security =>internet \ "Initialize and script ActiveX controls that are not marked as secure, set to Enabled \"");return; } }//Create an Ax object ExcelvarOWB =oXL.Workbooks.Add ();//Get Workbook Objectvarosheet =Owb.activesheet;//activating the current sheetvarSEL =Document.body.createTextRange (); Sel.movetoelementtext (CURTBL);//Move the contents of the table into the TextRangeSel.select ();//Select all the contents of TextRangeSel.execcommand ("Copy"); //copy content in TextRangeosheet.paste ();//paste into active ExceloXL.Visible =true; //set Excel Visible Properties}</script>onclick= "Javascript:datatoexcel (' Ta ')"/></body>Reference: http://blog.csdn.net/kalision/article/details/8350506
JS Export the table content in the Web page to Excel