为了方便用户打印众多格式不同的报表,笔者写了一段批量打印的算法,JS实现.主要原理是通过iFrame逐个获取待打印页面内容,然后将其导入到一个通用打印页面上,然后打印此页面.具体代码如下:
JS代码:
var win=null,arr=null;
var win2=null,pageURL=null;
var waitTime=50;
function PrintPage(arrPara,pageURLPara)
{
win = window.open("Print.aspx", "打印", "top=0,left=0,width=1024,height=800,toolbar=no, menubar=no, scrollbars=yes, resizable=yes,location=no, status=no");
arr = (arrPara==undefined||arrPara==null ? new Array():arrPara);
pageURL = (pageURLPara==undefined||pageURLPara==null ? null : pageURLPara);
window.setTimeout("CheckPrintData()",waitTime);
return win;
}
function CheckPrintData()
{
if(win.document.getElementById("ifmPageContext") == null)
{
window.setTimeout("CheckPrintData()",waitTime);
return;
}
if(pageURL != undefined && pageURL != null)
{
CheckInPage(0);
return;
}
if(document.all.form1 == null)
{
alert("页面没有名称为'form1'的Form元素,打印失败!");
return;
}
win.document.getElementById("Div_Data").innerHTML=document.all.form1.innerHTML;
for(var i=0; i<arr.length; i++)
{
if(win.document.getElementById(arr) != null)
{
win.document.getElementById(arr).style.display = "none";
}
}
win.document.getElementById("divHelper").style.display = 'none';
win.document.all.MyWebBrowser.ExecWB(7,1);
win.shouldExit = true;
}
function CheckInPage(index)
{
if(index<pageURL.length)
{
win.document.frames["ifmPageContext"].window.document.location.href = pageURL[index];
window.setTimeout("CheckInPageData(" + index + ")",waitTime);
}
else
{
win.document.frames["ifmPageContext"].src = "";
win.document.getElementById("divHelper").style.display = 'none';
win.document.all.MyWebBrowser.ExecWB(7,1);
win.shouldExit = true;
}
}
function CheckInPageData(index)
{
if(win.document.frames["ifmPageContext"].window.document.getElementById("form1") == null
|| win.document.frames["ifmPageContext"].window.document.location.href.indexOf(pageURL[index]) == -1)
{
window.setTimeout("CheckInPageData(" + index + ")",waitTime);
return;
}
for(var i=0; i<arr.length; i++)
{
var obj = win.document.frames["ifmPageContext"].window.document.getElementById(arr);
if(obj != null)
{
obj.style.display = "none";
}
}
win.document.getElementById("Div_Data").innerHTML += (index>0 ? "<div style='page-break-before: always'>" : "<div>");
win.document.getElementById("Div_Data").innerHTML += win.document.frames["ifmPageContext"].window.document.getElementById("form1").innerHTML;
win.document.getElementById("Div_Data").innerHTML += "</div>";
CheckInPage(index+1);
}
通用打印页面 [print.aspx] 代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Print.aspx.cs" Inherits="Print" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>通用打印页面</title>
<script type="text/javascript" language="javascript">
var shouldExit = false;
function Check()
{
if(shouldExit)
{
window.opener = null;
window.close();
}
}
</script>
</head>
<body onfocus="Check();">
<form id="form1" runat="server">
<object id="MyWebBrowser" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" height="0"
width="0">
</object>
<div id="divHelper">
<iframe id="ifmPageContext" name="ifmPageContext" width="100%" height="300px" style="display:none"></iframe>
<br />
<br />
<br />
页面加载中,请稍候...<br />
</div>
<div id="Div_Data">
</div>
</form>
</body>
</html>