龌龊但是省力的js输出excel

Function exportExcel(id){
   	try 
			{
				var oXL = new ActiveXObject("Excel.Application");
	    	}
			catch(e)
			{
				alert("无法调用Office对象\n 1请确保您的机器已安装了Office,\n 2请选择IE的Internet选项->安全->可信站点->站点\n 将本系统添加入可信站点中!");
				return ;
			}		
			
			oXL.Visible = true;
  			var oWB = oXL.Workbooks.Add(); 
  			var oSheet = oWB.ActiveSheet;  
  			var sel=document.body.createTextRange();
  			sel.moveToElementText(id);
  			sel.select();
  			sel.execCommand("Copy"); 
  			oSheet.Paste();
			//针对4列数据的格式设置
			oSheet.Cells(1,1).ColumnWidth = 20; 
			oSheet.Cells(1,2).ColumnWidth = 30; 
			oSheet.Cells(1,3).ColumnWidth = 5;
			oSheet.Cells(1,4).ColumnWidth = 20;
			oXL.Range("a1:d1").MergeCells = true;
			oSheet.Cells(1,1).Value =document.getElementById("stat_title").innerText;
			oXL.Range("a1:d1").Select;
			oXL.Selection.HorizontalAlignment = -4108;
			oXL.Selection.Font.Bold = true;
			oXL.Selection.Font.Size = 18;
			//oXL.execCommand("SaveAs");这个没有用
			//oXL.Quit();需要弹出保存时候使用
		}

你可能感兴趣的:(Excel,IE,Office)