批量打印

为了方便用户打印众多格式不同的报表,笔者写了一段批量打印的算法,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 />
                &nbsp; &nbsp; &nbsp; 页面加载中,请稍候...<br />
                &nbsp;
            </div>
            <div id="Div_Data">
            </div>
        </form>
    </body>
    </html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值