当前位置: 代码迷 >> Web前端 >> 兼容红狐 IE 的-导出Excel
  详细解决方案

兼容红狐 IE 的-导出Excel

热度:199   发布时间:2013-10-12 11:54:04.0
兼容火狐 IE 的---导出Excel
/** 导出Excel*/
	var exportTab = (function() {
		  var uri = 'data:application/vnd.ms-excel;base64,';
		  var 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></head><body><table>{table}</table></body></html>';
		    var base64 = function(s) {return window.btoa(unescape(encodeURIComponent(s))) };
		    var format = function(s, c) {return s.replace(/{(\w+)}/g,function(m, p) {return c[p]; }) };

			var explorer = window.navigator.userAgent ;
			if (explorer.indexOf("MSIE") >= 0) {
				return function(table,name){
					var curTbl = document.getElementById("listtable");
				     var oXL = new ActiveXObject("Excel.Application");
				     //创建AX对象excel
				     var oWB = oXL.Workbooks.Add();
				     //获取workbook对象
				    var oSheet = oWB.ActiveSheet;
				    //激活当前sheet
				     var Lenr = curTbl.rows.length;
				     //取得表格行数
				     for (i = 0; i < Lenr; i++)
				     {
				         var Lenc = curTbl.rows(i).cells.length;
				         //取得每行的列数
				         for (j = 0; j < Lenc; j++)
				         {
				             oSheet.Cells(i + 1, j + 1).value = curTbl.rows(i).cells(j).innerText;
				             //赋值
				         }
				     }
				     oXL.Visible = true;
				     //设置excel可见属性
				}
				
			}else{
				return function(table, name) {
			    if (!table.nodeType) table= document.getElementById(table)
			    var ctx = {worksheet: name|| 'Worksheet', table: table.innerHTML}
			    alert(table.innerHTML);
			    window.location.href = uri+ base64(format(template, ctx))
			  }
			}
		})();
  相关解决方案