Jsp的table导出Excel
1 var idTmr; 2 function getExplorer() { 3 var explorer = window.navigator.userAgent ; 4 //ie 5 if (explorer.indexOf("MSIE") >= 0) { 6 return 'ie'; 7 } 8 //firefox 9 else if (explorer.indexOf("Firefox") >= 0) { 10 return 'Firefox'; 11 } 12 //Chrome 13 else if(explorer.indexOf("Chrome") >= 0){ 14 return 'Chrome'; 15 } 16 //Opera 17 else if(explorer.indexOf("Opera") >= 0){ 18 return 'Opera'; 19 } 20 //Safari 21 else if(explorer.indexOf("Safari") >= 0){ 22 return 'Safari'; 23 } 24 } 25 26 function exportToExcel(tableid ) {//整个表格拷贝到EXCEL中 27 if(getExplorer()=='ie'){ 28 var curTbl = document.getElementById(tableid); 29 var oXL = new ActiveXObject("Excel.Application"); 30 31 //创建AX对象excel 32 var oWB = oXL.Workbooks.Add(); 33 //获取workbook对象 34 var xlsheet = oWB.Worksheets(1); 35 //激活当前sheet 36 var sel = document.body.createTextRange(); 37 sel.moveToElementText(curTbl); 38 //把表格中的内容移到TextRange中 39 sel.select; 40 //全选TextRange中内容 41 sel.execCommand("Copy"); 42 //复制TextRange中内容 43 xlsheet.Paste(); 44 //粘贴到活动的EXCEL中 45 oXL.Visible = true; 46 //设置excel可见属性 47 48 try { 49 var fname = oXL.Application.GetSaveAsFilename("Excel.xls", "Excel Spreadsheets (*.xls), *.xls"); 50 } catch (e) { 51 print("Nested catch caught " + e); 52 } finally { 53 oWB.SaveAs(fname); 54 oWB.Close(savechanges = false); 55 //xls.visible = false; 56 oXL.Quit(); 57 oXL = null; 58 //结束excel进程,退出完成 59 //window.setInterval("Cleanup();",1); 60 idTmr = window.setInterval("Cleanup();", 1); 61 } 62 }else{ 63 tableToExcel(tableid) 64 } 65 } 66 67 function Cleanup() { 68 window.clearInterval(idTmr); 69 CollectGarbage(); 70 } 71 72 var tableToExcel = (function() { 73 var uri = 'data:application/vnd.ms-excel;base64,', 74 template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" ' 75 +'xmlns:x="urn:schemas-microsoft-com:office:excel" ' 76 +'xmlns="http://www.w3.org/TR/REC-html40">' 77 +'<head>' 78 +'<!--[if gte mso 9]>' 79 +'<xml>' 80 +'<x:ExcelWorkbook>' 81 +'<x:ExcelWorksheets>' 82 +'<x:ExcelWorksheet>' 83 +'<x:Name>{worksheet}</x:Name>' 84 +'<x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions>' 85 +'</x:ExcelWorksheet>' 86 +'</x:ExcelWorksheets>' 87 +'</x:ExcelWorkbook>' 88 +'</xml>' 89 +'<![endif]-->' 90 +'</head>' 91 +'<body><table>{table}</table></body>' 92 +'</html>', 93 94 base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }, 95 format = function(s, c) { 96 return s.replace(/{(\w+)}/g, 97 function(m, p) { return c[p]; }) } 98 99 return function(table, name) { 100 if (!table.nodeType) table = document.getElementById(table) 101 var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML} 102 window.location.href = uri + base64(format(template, ctx)) 103 } 104 })()
转:http://www.cnblogs.com/xiaofengyuan/p/6133941.html
每一个不曾起舞的日子,都是对生命的辜负。