标签:
1 <html> 2 <head> 3 <script type="text/javascript" language="javascript"> 4 var idTmr; 5 function method1(tableid) {//整个表格拷贝到EXCEL中 6 var curTbl = document.getElementById(tableid); 7 var oXL = new ActiveXObject("Excel.Application"); 8 //创建AX对象excel 9 var oWB = oXL.Workbooks.Add(); 10 //获取workbook对象 11 var xlsheet = oWB.Worksheets(1); 12 //激活当前sheet 13 var sel = document.body.createTextRange(); 14 sel.moveToElementText(curTbl); 15 //把表格中的内容移到TextRange中 16 sel.select(); 17 //全选TextRange中内容 18 sel.execCommand("Copy"); 19 //复制TextRange中内容 20 xlsheet.Paste(); 21 //粘贴到活动的EXCEL中 22 oXL.Visible = true; 23 //设置excel可见属性 24 25 try { 26 var fname = oXL.Application.GetSaveAsFilename("将Table导出到Excel.xls", "Excel Spreadsheets (*.xls), *.xls"); 27 } catch (e) { 28 print("Nested catch caught " + e); 29 } finally { 30 oWB.SaveAs(fname); 31 32 oWB.Close(savechanges = false); 33 //xls.visible = false; 34 oXL.Quit(); 35 oXL = null; 36 //结束excel进程,退出完成 37 //window.setInterval("Cleanup();",1); 38 idTmr = window.setInterval("Cleanup();", 1); 39 40 } 41 } 42 function Cleanup() { 43 window.clearInterval(idTmr); 44 CollectGarbage(); 45 } 46 </script> 47 48 </head> 49 <body> 50 <table id="ta"> 51 <tr> 52 <td>1</td><td>admin</td> 53 <td>23</td><td>程序员</td> 54 <td>天津</td><td>admin@kali.com</td> 55 </tr> 56 <tr> 57 <td>2</td><td>guest</td> 58 <td>23</td><td>测试员</td> 59 <td>北京</td><td>guest@kali.com</td> 60 </tr> 61 </table> 62 <input id="Button1" type="button" value="导出EXCEL" 63 onclick="javascript:method1(‘ta‘)" /> 64 </body> 65 </html>
或
<html> <head> <script type="text/javascript"> function dataToExcel(tableid) {//整个表格拷贝到EXCEL中 var curTbl = document.getElementById(tableid); var oXL; try { oXL = GetObject("", "Excel.Application"); } catch (E) { try { oXL = new ActiveXObject("Excel.Application"); } catch (E2) { //alert("Please confirm:\n1.Microsoft Excel has been installed.\n2.Internet Options=>Security=>Setting \"Enable unsafe ActiveX\""); alert("请确认:\n1.机器上Excel已经安装.\n2.Internet 选项=>安全=>Internet \"对 未标记为安全的ActiveX控件进行初始化和脚本运行,设定为启用\""); return; } } //创建AX对象excel var oWB = oXL.Workbooks.Add(); //获取workbook对象 var oSheet = oWB.ActiveSheet; //激活当前sheet var sel = document.body.createTextRange(); sel.moveToElementText(curTbl); //把表格中的内容移到TextRange中 sel.select(); //全选TextRange中内容 sel.execCommand("Copy"); //复制TextRange中内容 oSheet.Paste(); //粘贴到活动的EXCEL中 oXL.Visible = true; //设置excel可见属性 } </script> </head> <body> <table id="ta"> <tr> <td>1</td><td>admin</td> <td>23</td><td>程序员</td> <td>天津</td><td>admin@kali.com</td> </tr> <tr> <td>2</td><td>guest</td> <td>23</td><td>测试员</td> <td>北京</td><td>guest@kali.com</td> </tr> </table> <input id="Button1" type="button" value="导出EXCEL" onclick="javascript:dataToExcel(‘ta‘)" /> </body> </html>
参考:http://blog.csdn.net/kalision/article/details/8350506
标签:
原文地址:http://www.cnblogs.com/myboke/p/4778968.html