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');
	}
	

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值