How to export Table to Excel using JavaScript

Source: Internet
Author: User

Copy codeThe Code is as follows:
Function copyToExcel (tableid ){
// Control button
Var btn = document. getElementById ("copy ");
Btn. setAttribute ("disabled", "true ");
Btn. setAttribute ("value", "processing ...");

Var curTbl = document. getElementById (tableid );
Try {
Var oXL = new ActiveXObject ("Excel. Application ");
}
Catch (e) {// if the security level of IE is not set, an error will occur (the Automation server cannot create an object)
/*
If Scripting. FileSystemObject (FSO text file read/write) is disabled, enable the FSO function and execute regsvr32 scrrun. dll in "run ".
*/
Alert ("cannot start Excel! \ N if you are sure that Excel, "+" is installed on your computer, adjust the security level of IE. \ N specific operation: \ n "+" Tools → Internet Options → Security → User-Defined Level → initialize and Run ActiveX programs not marked as secure → enable ");
Return false;
}
Var oWB = oXL. Workbooks. Add ();
Var oSheet = oWB. ActiveSheet;
Var sel = document. body. createTextRange ();
Sel. moveToElementText (curTbl );
Sel. select ();
Sel.exe cCommand ("Copy ");
OSheet. Paste ();
OXL. Visible = true;
Var fname = oXL. Application. GetSaveAsFilename ("Export tableto excel.xls", "Excel Spreadsheets (*. xls), *. xls ");
OWB. SaveAs (fname );
OWB. Close ();
OXL. Quit ();
// Control button
Btn. removeAttribute ("disabled ");
Btn. setAttribute ("value", "Export result to Excel ");
}

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.