JS导出HTML内容到Excel

var idTmr;
	function exportExcel(formName)
	{
		var oXL,oWB,oSheet;
		
		try{
			oXL = new ActiveXObject("Excel.Application");
		}catch(e){
			alert("无法启动Excel!\n\n如果您确信您的电脑中已经安装了Excel,那么调整IE的安全级别。\n\n具体操作:\n\n工具 → Internet选项 → 安全 → 自定义级别 → 对没有标记为安全的ActiveX进行初始化和脚本运行 → 启用");	
			return false;
		}
		oXL.DisplayAlerts = false;
		
		var otable = document.getElementById(formName);
		
		oWB = oXL.Workbooks.Add();
		oSheet = oWB.ActiveSheet;
		
		var rowCount = otable.rows.length;
		for(i = 2;i < rowCount;i++)
		{
			var colCount = otable.rows(i).cells.length;
			for(j = 1; j < colCount; j++)
			{
				oSheet.Cells(i-1,j).value = otable.rows(i).cells(j).innerText;	
			}
		}
		
		try{
			var fname = oXL.Application.GetSaveAsFilename("Save.xls","Excel Spreadsheets (*.xls), *.xls");	
		}catch(e){
			print("Nested catch caught " + e);	
		}finally{
			oWB.SaveAs(fname);	
			oWB.close(savechanges=false);
			oXL.Quit();
			oXL = null;
			//结束excel进程
			idTmr = window.setInterval("Cleanup();",1);
		}
	}
	
	function Cleanup(){
		window.clearInterval(idTmr);
		CollectGarbage();
	}

 

你可能感兴趣的:(JavaScript,js,导出Excel)