Home  >  Article  >  Web Front-end  >  Implementation ideas and code for exporting Table to Excel using JavaScript_javascript skills

Implementation ideas and code for exporting Table to Excel using JavaScript_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:40:45992browse
Copy code The 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 IE security level is not set, an error will occur (Automation server cannot create objects)
/*
If Scripting.FileSystemObject (FSO text file reading and writing) is turned off, just turn on the FSO function, in "Run" Execute regsvr32 scrrun.dll
*/
alert("Excel cannot be started!nn If you are sure that Excel is already installed on your computer," "then please adjust the security level of IE. nSpecific operations: n" "Tools → Internet Options → Security → Custom Level → Initialize and script ActiveX not marked as safe → Enable");
return false;
}
var oWB = oXL.Workbooks .Add();
var oSheet = oWB.ActiveSheet;
var sel = document.body.createTextRange();
sel.moveToElementText(curTbl);
sel.select();
sel.execCommand("Copy");
oSheet.Paste();
oXL.Visible = true;
var fname = oXL.Application.GetSaveAsFilename("Export table to excel.xls", "Excel Spreadsheets (*.xls), *.xls");
oWB.SaveAs(fname);
oWB.Close();
oXL.Quit();
//Control button
btn.removeAttribute("disabled");
btn.setAttribute("value", "Export results to Excel");
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn