前台呼叫(第一個參數是table的id): <input value="導出" type="button"> <br>function toExcel(inTblId, inWindow) { <br>if ($.browser.msie) { //如果是IE瀏覽器<br>try { <br>var allStr = ""; <br>var curStr = ""; <br>if (inTblId != null && inTblId != "" && inTblId != "null" ) { <br>curStr = getTblData(inTblId, inWindow); <br>} <br>if (curStr != null) { <br>allStr = curStr; <br>} <br>else { <br>alert( "你要匯出的表格不存在!"); <br>return; <br>} <br>var fileName = getExcelFileName(); <br>doFileExport(fileName, allStr); <br>} <br>catch ( e) { <br>alert("導出發生異常:" e.name "->" e.description "!"); <br>} <br>} <br>else { <br>window.open(' data:application/vnd.ms-excel,' encodeURIComponent($('div[id$=divGvData]').html())); <br>e.preventDefault(); <br>} <br>} <br>function getTblData(inTbl, inWindow) { <br>var rows = 0; <br>var tblDocument = document; <br>if (!!inWindow && inWindow != "") { <br>if (!document. all(inWindow)) { <br>return null; <br>} <br>else { <br>tblDocument = eval(inWindow).document; <br>} <br>} <br>var curTbl =Document. (inTbl); <br>if (curTbl.rows.length > 65000) { <br>alert('來源行數不能大於65000行'); <br>return false; <br>} <br>if (curTbl .rows.length alert('資料來源沒有資料'); <br>return false; <br>} <br>var outStr = ""; <br>if (curTbl != null ) { <br>for (var j = 0; j for (var i = 0; i if (i == 0 && rows > 0) { <br>outStr = " t"; <br>rows -= 1; <br>} <br>var tc = curTbl.rows[j].cells[ i]; <br>if (j > 0 && tc.hasChildNodes() && tc.firstChild.nodeName.toLowerCase() == "input") { <br>if (tc.firstChild.type.toLowerCase() == <br>if (tc.firstChild.type.toLowerCase() == "checkbox") { <br>if (tc.firstChild.checked == true) { <br>outStr = "是" "t"; <br>} <br>else { <br>outStr = "否" " t"; <br>} <br>} <br>} <br>else { <br><br>outStr = " " curTbl.rows[j].cells[i].innerText "t"; <br> } <br>if (curTbl.rows[j].cells[i].colSpan > 1) { <br>for (var k = 0; k outStr = " t"; <br>} <br>} <br>if (i == 0) { <br>if (rows == 0 && curTbl.rows[j].cells [i].rowSpan > 1) { <br>rows = curTbl.rows[j].cells[i].rowSpan - 1; <br>} <br>} <br>} <br>outStr = "rn" ; <br>} <br>} <br>else { <br>outStr = null; <br>alert(inTbl "不存在!"); <br>} <br>return outStr; <br>} <br>} <br>return outStr; <br>} <br>} <br>return outStr; <br>} <br>} <br>return outStr; <br>} <br>} <br>return outStr; <br>} <br>} <br>return outStr; <br>} <br>} <br>function getExcelFileName() { <br>var d = new Date(); <br>var curYear = d.getYear(); <br>var curMonth = "" (d.getMonth() 1); <br>var curDate = "" d.getDate(); <br>var curHour = "" d.getHours(); <br>var curMinute = "" d.getMinutes(); <br>var curSecond = "" d.getSeconds( ); <br>if (curMonth.length == 1) { <br>curMonth = "0" curMonth; <br>} <br>if (curDate.length == 1) { <br>curDate = "0" curDate; <br>} <br>if (curHour.length == 1) { <br>curHour = "0" curHour; <br>} <br>if (curMinute.length == 1) { <br>curMinute = "0" curMinute; <br>} <br>if (curSecond.length == 1) { <br>curSecond = "0" curSecond; <br>} <br>var fileName = "裝置狀態" curYear; <br>} <br>var fileName = "裝置狀態" curYear curMonth curDate🎜>var fileName = "裝置狀態" curYear curMonth curDate🎜>var fileName = "裝置狀態" curYear curMonth curDate curMonth curDate curHour curMinute curSecond ".xls"; <br>return fileName; <br>} <br>function doFileExport(inName, inStr) { <br>var xlsWin = null;
登入後複製
if (!.doc ")) { xlsWin = glbHideFrm; } else { var width = 1; var height = 1; var openPara = "left=" (window.screen .width / 2 width / 2) ",top=" (window.screen.height height / 2) ",scrollbars=no,width=" width ",height=" height; xlsWin = window.open("", "_blank", openPara); } xlsWin.document.write(inStr); xlsWin.document.close(); xlsWin.document.exesWin.document.close(); xlsWin.document.execCommand ('Saveas', true, inName); xlsWin.close(); }