Home  >  Article  >  Web Front-end  >  js export table to excel compatible with FF and IE examples_javascript skills

js export table to excel compatible with FF and IE examples_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:23:361250browse
Copy code The code is as follows:

 Foreground call (the first parameter is the id of the table): <input value="export" type="button" /> <br>function toExcel(inTblId, inWindow) { <br>if ($.browser.msie ) { //If it is IE browser <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("The table you want to export does not exist!"); <br>return; <br>} <br>var fileName = getExcelFileName(); <br>doFileExport(fileName, allStr); <br>} <br>catch (e) { <br>alert("Export exception:" 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('The number of source rows cannot be greater than 65000 rows'); <br>return false; <br>} <br>if (curTbl.rows.length <= 1) { <BR>alert('Data source has no data'); <BR>return false; <BR>} <BR>var outStr = ""; <BR>if (curTbl != null) { <BR>for (var j = 0; j < curTbl.rows.length; j ) { <BR>for (var i = 0; i < curTbl.rows[j] .cells.length; i ) { <BR>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 = "Yes" "t"; <br>} <br> else { <br>outStr = "No" "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 < curTbl. rows[j].cells[i].colSpan - 1; k ) { <BR>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 "Does not exist!"); <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 = "Device Status" 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>} 

< ;/pre> 


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn