JavaScript Exports Web page table data to Excel files

Source: Internet
Author: User
Tags base64

Do not use the server-side technology, directly using JS to export the tabular data in the Web page as an Excel file, support all browsers;
The prerequisite is that the table data in the Web page must be formatted with the table label and no errors can be found.

<!DOCTYPE HTML><HTML><Head>    <title>JavaScript exportexcel</title></Head><Body>    <TableID= "Name"Border= "1"cellspacing= "0"cellpadding= "Ten">        <TR>            <th>Name</th>            <th>Age</th>            <th>Job</th>        </TR>        <TR>            <TD>Mike</TD>            <TD>22</TD>            <TD>Designer</TD>        </TR>        <TR>            <TD>David</TD>            <TD>28</TD>            <TD>Programmer</TD>        </TR>    </Table>    <BR>    <Buttononclick= "Exportexcel (' name ');">Export to Excel</Button><Body><Script>    /** * Export tables in HTML to Excel (compatible with all browsers) * @param {[type]} tableId [ID of table]*/    functionExportexcel (tableId) {varIDTMR; if (/msie|trident/i.test (navigator.useragent)) {            varCurtbl=document.getElementById (tableId), OXL= NewActiveXObject ('Excel.Application'), oWB=oXL.Workbooks.Add (),//Create an Ax object ExcelXlsheet=Owb.worksheets (1),                  //Get Workbook Objectsel=Document.body.createTextRange (); //activating the current sheetSel.movetoelementtext (CURTBL);//Move the contents of the table into the TextRangeSel.execcommand ("Copy"); //copy content in TextRangeXlsheet.               Paste (); //paste into active Exceloxl.visible= true; //set Excel Visible Properties              Try {                varfname=OXL.Application.GetSaveAsFilename ('Excel.xls', 'Excel Spreadsheets (*.xls), *.xls'); } Catch(e) {print ('Nested Catch caught' +e); } finally{owb.saveas (fname); Owb.close (SaveChanges= false);                Oxl.quit (); OXL= NULL; IDTMR=SetInterval (function() {clearinterval (IDTMR);                CollectGarbage (); }, 1); }        } Else {            functionBase64 (s) {returnBtoa (unescape (encodeURIComponent (s))}; functionformat (s, c) {returnS.replace (/{(\w+)}/G,function(M, p) {returnC[p];            });              }; varURI= 'Data:application/vnd.ms-excel;base64,', Template=                 '' +                    '' +                        '<!--[if GTE MSO 9]><xml><x:excelworkbook><x:excelworksheets><x:excelworksheet> <x:name>{worksheet}</x:name><x:worksheetoptions><x:displaygridlines/></x: Worksheetoptions></x:excelworksheet></x:excelworksheets></x:excelworkbook></xml> <! [endif]-->' +                    '' +                    '<body>' +                        '<table>{table}</table>' +                    '</body>' +                '', CTX={worksheet:name|| 'Worksheet', Table:document.getElementById (tableId). InnerHTML}; Location.href=URI+base64 (Format (template, CTX)}}</Script></HTML>

JavaScript Exports Web page table data to Excel files

Related Article

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.