Heim > Web-Frontend > js-Tutorial > js导出table到excel同时兼容FF和IE示例_javascript技巧

js导出table到excel同时兼容FF和IE示例_javascript技巧

WBOY
Freigeben: 2016-05-16 17:23:36
Original
1336 Leute haben es durchsucht
复制代码 代码如下:

前台调用(第一个参数是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 = tblDocument.getElementById(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() == "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 += "\r\n"; <br>} <br>} <br>else { <br>outStr = null; <br>alert(inTbl + "不存在!"); <br>} <br>return outStr; <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 + curMonth + curDate + curHour + curMinute + curSecond + ".xls"; <br>return fileName; <br>} <br>function doFileExport(inName, inStr) { <br>var xlsWin = null; <br>if (!!document.all("glbHideFrm")) { <br>xlsWin = glbHideFrm; <br>} <br>else { <br>var width = 1; <br>var height = 1; <br>var openPara = "left=" + (window.screen.width / 2 + width / 2) <br>+ ",top=" + (window.screen.height + height / 2) <br>+ ",scrollbars=no,width=" + width + ",height=" + height; <br>xlsWin = window.open("", "_blank", openPara); <br>} <br>xlsWin.document.write(inStr); <br>xlsWin.document.close(); <br>xlsWin.document.execCommand('Saveas', true, inName); <br>xlsWin.close(); <br>} 
Nach dem Login kopieren

Nach dem Login kopieren



Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage