zoukankan      html  css  js  c++  java
  • asp.net中如何打印ReportViewer报表

         
    .net 2.0中的新控件ReportViewer可以方便的制作并显示报表,但是它没有直接支持在网页中的打印。我在分析网页HTML源代码的基础上找到了直接打印的诀窍,先做成一个函数,方便直接使用。

        1.包含ReportViewer报表的网页的最终形式HTML DOM结构中,报表被放到一个<iframe>中,其id命名方式为:"ReportFrame"+报表控件id;
        2.报表内容被放到包含在1中的另一个<iframe>中,其id固定为:"report";
        3.为了实现打印,我们只要先获取内容<iframe>对象,设置焦点,然后调用print方法打印即可。
        4.已经封装好的javascript函数如下:

    // JScript 文件
    //
    要打印ReportView报表的内容,只需要引用本文件,然后调用PrintReportView()函数即可。
    //
    例如:在某按钮的点击事件中包括代码,onclick="PrintReportView(window,'ReportViewerYsqd');"

    //得到ReportView控件生成的客户端代码的报表内容区的FRAME对象
    //
    参数:objWindow——包含ReportView控件的window对象
    //
          strReportViewerId——需要被打印的ReportViewer控件的ID
    //
    返回:得到的报表内容区FRAME对象;如果获取失败,返回null。
    function GetReportViewContentFrame(objWindow,strReportViewerId)
    {
        
    var frmContent=null;    //报表内容区对象的FRAME对象
        var strFrameId="ReportFrame" + strReportViewerId ;  //asp.net自动生成的iframe 的id为:ReportFrame+报表控件id
        try
        
    {
            frmContent
    =window.frames[strFrameId].frames["report"];  //报表内容框架的id为report
        }

        
    catch(e)
        
    {
        }

        
    return frmContent;
    }


    //打印ReportView控件中的报表内容
    //
    参数:objWindow——包含ReportView控件的window对象
    //
          strReportViewerId——需要被打印的ReportViewer控件的ID
    //
    返回:(无)
    function PrintReportView(objWindow,strReportViewerId)
    {
        
    var frmContent=GetReportViewContentFrame(objWindow,strReportViewerId);
        
    if(frmContent!=null && frmContent!=undefined)
        
    {
            frmContent.focus();
            frmContent.print();
        }

        
    else
        
    {
            alert(
    "在获取报表内容时失败,无法通过程序打印。如果要手工打印,请鼠标右键点击报表内容区域,然后选择菜单中的打印项。");
        }

    }

        5.下面是一个测试打印报表的完整例子:

    <%@ Page Language="C#" %>
    <%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
        Namespace
    ="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <script runat="server">
    </script>
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        
    <title>无标题页</title>
    </head>
    <body>
        
    <form id="form1" runat="server">
        
    <div>
            
    <rsweb:ReportViewer ID="rvYhqd" runat="server" Font-Names="Verdana" Font-Size="8pt" Height="484px" Width="718px">
                
    <LocalReport ReportPath="公用程序yhqd.rdlc">
                
    </LocalReport>
            
    </rsweb:ReportViewer>
            
    <br />
            
    <asp:Button ID="Button1" runat="server" Text="打印" OnClientClick="return PrintReport();" />
        
    </div>
        
    </form>
        
    <!--这里引用了包含打印报表函数的js文件-->
        
    <script language="javascript" type="text/javascript" src="ReportView.js"></script>
        
    <script language="javascript" type="text/javascript">
        
    <!--
            
    //打印报表
            function PrintReport()
            
    {
                PrintReportView(window,
    "rvYhqd");
                
    return false;
            }

        
    //-->
        </script>
    </body>
    </html>
  • 相关阅读:
    好文章记录
    求职经历
    C正确初始化方式
    linux 常用命令
    MYSQL查找从小到大排列第90%个位置的数据
    最好的单例模式
    <%= %>和${}使用差异
    后台和jsp乱码处理
    浏览器下载文件
    文件下载
  • 原文地址:https://www.cnblogs.com/chance/p/836152.html
Copyright © 2011-2022 走看看