IE浏览器使用JS技术导出到Excel和打印

          在一些要求不是很高的小项目中,可以使用一些虽不是通用且不是新技术但是确实可以很好实现功能的技术来实现这些功能。这样系统不是显示的很复杂,且可以方便维护。下面就简单介绍一种技术,可以使用简单的JS来实现把网页中的信息原样导出到Excel、还可以打印。

 

        新建一个exportPrint.html页面,里面的代码如下所示,就可以实现导出到Excel和打印网页。

<html>
	<head>
		<title>IE浏览器使用JS技术导出到Excel和打印</title>
		<style>
			.table_stat {
				border-right:0px;
				border-bottom:0px;
				border-left:1px solid #819BD8;
				border-top:1px solid #819BD8;
			}
			.td_stat {
				border-right:1px solid #819BD8;
				border-bottom:1px solid #819BD8;
			}
		</style>
	</head>
	<body>
		<object classid="CLSID:8856F961-340A-11DO-A96B-00C04FD705A2" height="0" id="WebBrowser" width="0"></object>
		<table width="100%" align="center" border="0" cellpadding="0" cellspacing="0" style="text-align: center;" class="table_stat">
			<tr>
				<td id="title" align="center" nowrap="nowrap" class="td_stat" colspan="2">
					用户信息
				</td>
			</tr>
			<tr>
				<td id="title" align="center" nowrap="nowrap" class="td_stat" colspan="1">
					姓名
				</td>
				<td id="title" align="center" nowrap="nowrap" class="td_stat" colspan="1">
					张三
				</td>
			</tr>
			
			<tr>
				<td id="title" align="center" nowrap="nowrap" class="td_stat" colspan="2">
					<input type="button" id="export" value="导出" οnclick="javascript:exportToExcel();" >
					<input type="button" id="print" value="打印" οnclick="javascript:print();" >
				</td>
			</tr>
		</table>
	</body>
</html>

<script type="text/javaScript">
	//导出到Excel
	function exportToExcel() {
		if(document.getElementById("title")) {
			try {
				var oRangeRef = document.body.createTextRange();
				oRangeRef.execCommand("Copy");
				var appExcel = new ActiveXObject("Excel.Application");
				appExcel.visible = true;
				appExcel.Workbooks.Add().WorkSheets.Item(1).Paste();
			} catch(e) {
				alert("出错啦!可能是浏览器或者是数据量太大咯哦!");
				return;
			}
			appExcel = null;
			oRangeRef = null;
		}
	}
	
	//打印
	function print() {
		if(document.getElementById("title")) {
			var export = document.getElementById("export");
			var print = document.getElementById("print");
			try {
				export.style.display = "none";
				print.style.display = "none";
				document.all.WebBrowser.ExecWB(6,1);
			} catch(e) {
				alert("出错啦!可能是浏览器或者是数据量太大咯哦!");
				return;
			}
			export.style.display = "";
			print.style.display = "";
		}
	}
</script>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值