站长技术网

首页 > 网站基础 > HTML教程 >

ie浏览器使用js导出网页到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="导出" onclick="javascript:exportToExcel();" >

<input type="button" id="print" value="打印" onclick="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>