JS Export the table content in the Web page to Excel

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.