1 function getXlsFromTbl(inTblId, inWindow, fname) { 2 try { 3 var allStr = ""; 4 var curStr = ""; 5 // alert("getXlsFromTbl"); 6 if (inTblId != null && inTblId != "" && inTblId != "null") { 7 curStr = getTblData(inTblId, inWindow); 8 } 9 // alert("curStr="+curStr); 10 if (curStr != null) { 11 allStr += curStr; 12 } else { 13 alert("你要导出的表格不存在!"); 14 return; 15 } 16 var fileName = getExcelFileName(fname); 17 doFileExport(fileName, allStr); 18 } catch (e) { 19 alert("导出发生异常:" + e.name + "->" + e.description + "!"); 20 } 21 } 22 function getTblData(inTbl, inWindow) { 23 var rows = 0; 24 // alert("getTblData is " + inTbl + "==" + inWindow); 25 var tblDocument = document; 26 if (!!inWindow && inWindow != "") { 27 if (!document.all(inWindow)) { 28 return null; 29 } else { 30 tblDocument = eval(inWindow).document; 31 } 32 } 33 var curTbl = tblDocument.getElementById(inTbl); 34 var outStr = ""; 35 if (curTbl != null) { 36 for (var j = 0; j < curTbl.rows.length; j++) { 37 // alert("j is " + j); 38 for (var i = 0; i < curTbl.rows[j].cells.length; i++) { 39 // alert("i is " + i); 40 if (i == 0 && rows > 0) { 41 outStr += " "; 42 rows -= 1; 43 } 44 outStr += curTbl.rows[j].cells[i].innerText + " "; 45 // alert(); 46 if (curTbl.rows[j].cells[i].colSpan > 1) { 47 for (var k = 0; k < curTbl.rows[j].cells[i].colSpan - 1; k++) { 48 outStr += " "; 49 } 50 } 51 if (i == 0) { 52 if (rows == 0 && curTbl.rows[j].cells[i].rowSpan > 1) { 53 rows = curTbl.rows[j].cells[i].rowSpan - 1; 54 } 55 } 56 } 57 if (j == 0) 58 outStr = replaceAll(outStr, " ", ""); //替换掉回车换行 59 outStr += " "; 60 // alert(outStr); 61 } 62 } else { 63 outStr = null; 64 alert(inTbl + "不存在!"); 65 } 66 return outStr; 67 } 68 function getExcelFileName(fname) { 69 var d = new Date(); 70 var curYear = d.getYear(); 71 var curMonth = "" + (d.getMonth() + 1); 72 var curDate = "" + d.getDate(); 73 var curHour = "" + d.getHours(); 74 var curMinute = "" + d.getMinutes(); 75 var curSecond = "" + d.getSeconds(); 76 if (curMonth.length == 1) { 77 curMonth = "0" + curMonth; 78 } 79 if (curDate.length == 1) { 80 curDate = "0" + curDate; 81 } 82 if (curHour.length == 1) { 83 curHour = "0" + curHour; 84 } 85 if (curMinute.length == 1) { 86 curMinute = "0" + curMinute; 87 } 88 if (curSecond.length == 1) { 89 curSecond = "0" + curSecond; 90 } 91 var fileName = fname + "_" + curYear + curMonth + curDate + "_" + curHour 92 + curMinute + curSecond + ".csv.html"; 93 // alert(fileName); 94 return fileName; 95 } 96 function doFileExport(inName, inStr) { 97 var xlsWin = null; 98 if (!!document.all("glbHideFrm")) { 99 xlsWin = glbHideFrm; 100 } else { 101 var width = 5; 102 var height = 5; 103 var openPara = "left=" + (window.screen.width / 2 - width / 2) 104 + ",top=" + (window.screen.height / 2 - height / 2) 105 + ",scrollbars=no,width=" + width + ",height=" + height; 106 xlsWin = window.open("", "_blank", openPara); 107 } 108 xlsWin.document.write(inStr); 109 xlsWin.document.close(); 110 111 xlsWin.document.execCommand('Saveas', true, inName); 112 xlsWin.close(); 113 } 114 115 function replaceAll(ss, s, r) { 116 var rs = ss; 117 // alert(rs+"="+r); 118 // alert(rs.indexOf(s)); 119 while (rs.indexOf(s) != -1) { 120 // alert("A="+rs); 121 rs = rs.replace(s, r); 122 // alert("B="+rs); 123 } 124 return rs; 125 }