js 兼容Firefox与IE的Excel到处
2016-03-02 18:37
471 查看
//inTblId 待导出table的ID function toExcel(inTblId, inWindow) { if($.browser.msie) { //如果是IE浏览器 try { var allStr = ""; var curStr = ""; if (inTblId != null && inTblId != "" && inTblId != "null") { curStr = getTblData(inTblId, inWindow); } if (curStr != null) { allStr += curStr; } else { alert("你要导出的表不存在!"); return; } var fileName = getExcelFileName(); doFileExport(fileName, allStr); } catch (e) { alert("导出发生异常:" + e.name + "->" + e.description + "!"); } } else { tableToExcelForFirefox('tb', ''); } }
IE下执行
function doFileExport(inName, inStr) { var xlsWin = null; if (!!document.all("glbHideFrm")) { 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.execCommand('Saveas', true, inName); xlsWin.close(); }
FireFox
var tableToExcelForFirefox = (function () { var uri = 'data:application/vnd.ms-excel;base64,' , template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>' , base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) } , format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) } return function (table, name) { if (!table.nodeType) table = document.getElementById(table) var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML } window.location.href = uri + base64(format(template, ctx)) } })()
这是综合网上的办法,关键需要解决window.btoa IE 下兼容问题
相关文章推荐
- excel导出js代码
- BZOJ1821 [JSOI2010] Group 部落划分 Group
- 使用JS显示弹出窗口
- jsp导入的包,包里明明有方法,但总是报方法没有定义的错
- 【求助】js到处Excel window.btoa IE 下没有定义
- Js获取当前日期时间及其它操作
- underscore.js 分析 第四天
- JSP你必会的4+9
- JSPath基础用法
- js倒计时代码 适合于促销-倒计时代码
- 详细介绍Jsp中的Cookies
- 关于jsp的九大隐式
- jsp 获取cookie 的值的方法
- jsp创建cookie
- JSP中如何设置Cookies
- JSP Cookie 使用完全详解
- javascript 要点总结
- anjularjs弹出model(模态框)
- jsp的9大隐式对象的使用总结
- js 促销倒计时