当前位置: 代码迷 >> Web前端 >> IE网页导出word跟excel
  详细解决方案

IE网页导出word跟excel

热度:125   发布时间:2012-06-30 17:20:13.0
IE网页导出word和excel
function exportAsWord() {
		var flag = 1;
		try {
			var oWord = new ActiveXObject("Word.Application");
		} catch (e) {
			flag = 2;
			alert("请改变IE浏览器的安全设置!将'对没有标记为安全的ActiveX控件进行初始化和脚本运行'设为'启用'。");
		}
		if (flag == 1) {
			oWord.Application.Visible = true;
			var mydoc=oWord.Documents.Add('',0,1);
			myRange =mydoc.Range(0,1);
			//指定文件编辑位置
			var sel=document.body.createTextRange();
			var obj=document.getElementById("dataTable");
			try {
				if (obj) {
					sel.moveToElementText(obj);
				}
			} catch (e) {
			}
			sel.select();
			document.execCommand('Copy');
			sel.moveEnd('character');
			myRange.Paste();
			//设置页边距
			oWord.ActiveDocument.PageSetup.TopMargin = 60;
			oWord.ActiveDocument.PageSetup.BottomMargin = 60;
			oWord.ActiveDocument.PageSetup.LeftMargin = 50 ;
			oWord.ActiveDocument.PageSetup.RightMargin = 50 ;
			//设置页面方向
			oWord.ActiveDocument.PageSetup.Orientation = 0;
			oWord.Selection.Font.Size=20;
			document.execCommand('unselect');
			//设置文档为页面视图
			oWord.ActiveWindow.ActivePane.View.Type= 3;
		}
	}
 
	function exportAsExcel() {
		var oXL = new ActiveXObject("Excel.Application");
		var oWB = oXL.Workbooks.Add();
		var oSheet = oWB.ActiveSheet;
		var sel=document.body.createTextRange();
		sel.moveToElementText(dataTable);
		sel.select();
		sel.execCommand("Copy");
		oSheet.Paste();
		oXL.Visible = true;
		document.execCommand('unselect');
	}
	

?

  相关解决方案