js导出到excel 兼容ie11火狐,谷歌,360

<script language="javascript" type="text/javascript">         function isIE() { //ie?             if (!!window.ActiveXObject || "ActiveXObject" in window)                 return true;             else                 return false;         }         function getXlsFromTbl(inTblId, inWindow) {             try {                 var allStr = "";                 var curStr = "";                 var fileName = getExcelFileName();                 //                var cc = document.all;                 //                var ccc = typeof (document.all)                 //                alert(cc)                 //                alert(ccc)                 if (inTblId != null && inTblId != "" && inTblId != "null") {                     var hh = isIE();                     if (hh == true) //IE浏览器                     {                         curStr = getTblData(inTblId, inWindow);                         if (curStr != null) {                             allStr += curStr;                         }                         else {                             alert("你要导出的表不存在!");                             return;                         }                         doFileExport(fileName, allStr);                     } else {                         curStr = getTblData1(inTblId, inWindow);                         if (curStr != null) {                             allStr += curStr;                         }                         else {                             alert("你要导出的表不存在!");                             return;                         }                         var uri = ‘data:text/xls;charset=utf-8,\ufeff‘ + encodeURIComponent(allStr);                         //创建a标签模拟点击下载                         var downloadLink = document.createElement("a");                         downloadLink.href = uri;                         downloadLink.download = fileName;                         document.body.appendChild(downloadLink);                         downloadLink.click();                         document.body.removeChild(downloadLink);                     }                 }             }             catch (e) {                 alert("导出发生异常:" + e.name + "->" + e.description + "!");             }         }         function getTblData(inTbl, inWindow) {             var rows = 0;             //alert("getTblData is " + inWindow);             var tblDocument = document;             if (!!inWindow && inWindow != "") {                 if (!document.all(inWindow)) {                     return null;                 }                 else {                     tblDocument = eval(inWindow).document;                 }             }             var curTbl = tblDocument.getElementById(inTbl);             var outStr = "";             if (curTbl != null) {                 for (var j = 0; j < curTbl.rows.length; j++) {                     //alert("j is " + j);                     for (var i = 0; i < curTbl.rows[j].cells.length; i++) {                         //alert("i is " + i);                         if (i == 0 && rows > 0) {                             outStr += " \t";                             rows -= 1;                         }                         outStr += curTbl.rows[j].cells[i].innerHTML + " \t";                         if (curTbl.rows[j].cells[i].colSpan > 1) {                             for (var k = 0; k < curTbl.rows[j].cells[i].colSpan - 1; k++) {                                 outStr += " \t";                             }                         }                         if (i == 0) {                             if (rows == 0 && curTbl.rows[j].cells[i].rowSpan > 1) {                                 rows = curTbl.rows[j].cells[i].rowSpan - 1;                             }                         }                     }                     outStr += "\r\n";                 }             }             else {                 outStr = null;                 alert(inTbl + "不存在!");             }             return outStr;         }         function getTblData1(inTbl, inWindow) {             var rows = 0;             var tblDocument = document;             //tblDocument = eval(inWindow).document;             var curTbl = tblDocument.getElementById(inTbl);             var outStr = "";             if (curTbl != null) {                 for (var j = 0; j < curTbl.rows.length; j++) {                     for (var i = 0; i < curTbl.rows[j].cells.length; i++) {

if (i == 0 && rows > 0) {                             outStr += ","; // \t";                             rows -= 1;                         }

outStr += curTbl.rows[j].cells[i].innerHTML + ","; //\t";                         if (curTbl.rows[j].cells[i].colSpan > 1) {                             for (var k = 0; k < curTbl.rows[j].cells[i].colSpan - 1; k++) {                                 outStr += ","; // \t";                             }                         }                         if (i == 0) {                             if (rows == 0 && curTbl.rows[j].cells[i].rowSpan > 1) {                                 rows = curTbl.rows[j].cells[i].rowSpan - 1;                             }                         }                     }                     outStr += "\r\n";                 }             }

else {                 outStr = null;                 alert(inTbl + "不存在 !");             }             return outStr;         }         function getExcelFileName() {             var d = new Date();             var curYear = d.getYear();             var curMonth = "" + (d.getMonth() + 1);             var curDate = "" + d.getDate();             var curHour = "" + d.getHours();             var curMinute = "" + d.getMinutes();             var curSecond = "" + d.getSeconds();             if (curMonth.length == 1) {                 curMonth = "0" + curMonth;             }             if (curDate.length == 1) {                 curDate = "0" + curDate;             }             if (curHour.length == 1) {                 curHour = "0" + curHour;             }             if (curMinute.length == 1) {                 curMinute = "0" + curMinute;             }             if (curSecond.length == 1) {                 curSecond = "0" + curSecond;             }             var fileName = "百度一下你就知道" + "_" + curYear + curMonth + curDate + "_"             + curHour + curMinute + curSecond + ".xls";             //alert(fileName);             return fileName;         }         function doFileExport(inName, inStr) {             var xlsWin = null;

if (!!document.all("glbHideFrm")) {

xlsWin = glbHideFrm;

}             else {

var width = 6;

var height = 4;

var openPara = "left=" + (window.screen.width / 2 - width / 2)

+ ",top=" + (window.screen.height / 2 - 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();         }     </script>

时间: 2024-09-30 20:47:30

js导出到excel 兼容ie11火狐,谷歌,360的相关文章

Js 设置class,兼容ie,火狐的方式

var trs = document.getElementsByTagName("tr"); trs[0].className="color2";  //设置css样式. 不兼容火狐! 兼容火狐,ie的写法为: var cls = trs[0].getAttribute('class'); // 添加classtrs[0].setAttribute('class', cls + ' color3'); Js 设置class,兼容ie,火狐的方式,布布扣,bubuko

js 导出到excel 最新兼容火狐谷歌ie11

function isIE() { //ie?            if (!!window.ActiveXObject || "ActiveXObject" in window)                return true;            else                return false;        }        function getXlsFromTbl(inTblId, inWindow) {            try {    

js 导出到excel

/* * 默认转换实现函数,如果需要其他功能,需自行扩展 * 参数: * tableID : HTML中Table对象id属性值 * 详细用法参见以下 TableToExcel 对象定义 */ function saveAsExcel(tableID) { var tb = new TableToExcel(tableID); tb.setFontStyle("Courier New"); tb.setFontSize(10); tb.setTableBorder(2); tb.set

iframe自适应高度(兼容IE 火狐 谷歌)

<div id="leamain"> <iframe src="#" marginheight="0" marginwidth="0" frameborder="0" scrolling="no" width="765" height=100% id="iframepage" name="iframepage"

JavaScript将网页表格数据导出为Excel文件

不使用服务器端的技术,直接使用js将网页中的表格数据导出为excel文件,支持所有浏览器:前提条件是:网页中的表格数据必须使用table标签排版且不能有任何错误. <!DOCTYPE HTML> <html> <head> <title>javascript exportExcel</title> </head> <body> <table id="name" border="1&quo

js导出table中的EXCEL总结

导出EXCEL一般是用PHP做,但是项目中,有时候PHP后端工程师返回的数据不是我们想要的,作为前端开发工程师,把对应的数据编号转换为文字后,展示给用户,但是,需求要把数据同时导出一份EXCEl.无奈之下,我只能用js导出table中的数据了. 导出EXCEl一般是自己人用的,所以用js导出,因为js导出EXCEL一般情况下兼容性不是很好,很多只是兼容IE浏览器,还要设置在工具栏中进行设置才能导出,因为会相对比较烦.下面介绍几种方法: 一.js导出EXCEl带单元格合并[已验证,比较好用] //

JS导出数据到EXCEL

1.用到两个文件Blob.js和FileSaver.js 2.代码: <!DOCTYPE html> <html lang="zh"> <head> <meta charset="utf-8"/> <title>js导出excel-ComingX</title> </head> <body> <h1> </h1> <a> <p&g

兼容 谷歌、火狐、360系列浏览器桌面通知()有用

兼容 谷歌.火狐.360系列浏览器桌面通知(有用) 本文从总结工作,并且参照大量的网络资源的.我们希望有同样需求的朋友来帮忙. (部分): http://xsk.tehon.org/den/index.php/category/tech/html5-audio-notifications.html http://ttsvetko.github.io/HTML5-Desktop-Notifications/# http://www.cnblogs.com/meteoric_cry/archive/

js导出excel增加表头、mso-number-format定义数据格式

问题1:增加表头 js导出表格时,只会导出table里的展现出的内容,如需增加表头等内容需要在页面获取的字符串中拼接表头的相关字符串,详细介绍如下: tableString:新增的表头内容字符串: ctx.table: 新增的表头+页面获取的table字符串. 这样,表头就加上了,还可以通过style设置它的样式: 问题2:mso-number-format定义数据格式 在做项目时遇到mso-number-format定义的科学计数法转化格式与金额定义的向右对齐样式有冲突.  之前有写过篇博客专