在ASP.NET中使用SqlServer Reporting Service樣例

經過一年多努力,本月底項目進入試運行.在本次項目中使用SqlServer Reporting Service作爲報表開發的工具,現將操作記錄在此,無論報表還是單證其展示方式一般分爲兩種:
1、 在頁面中某個區域直接顯示
2、 點擊某個按鈕觸發後彈出新窗口顯示
爲此基類ApplicationBasePage.cs頁面提供瞭如下兩個方法

/// <summary>
    
/// 得到報表窗口的URL
    
/// </summary>
    
/// <param name="reportCode">報表代碼,如rp_pr_qa_001</param>
    
/// <param name="paraName">參數名稱數組,與參數值數組對應</param>
    
/// <param name="paraValue">參數值數組,與參數名稱數組對於</param>
    
/// <param name="viewFormat">顯示格式代碼,0:顯示參數、工具欄,1:不顯示參數、顯示工具欄,2:不顯示參數、工具欄</param>

    protected string GetReportUrl(string reportCode, string[] paraName, string[] paraValue, int viewFormat)
    
{
        
//報表的基礎地址
        
//string reportingServerUrlBase = "http://10.3.130.72/ReportServer?/BpmsReports/";//ReportServer
        string reportingServerUrlBase = ConfigurationManager.AppSettings["ReportServer"].ToString();
        
//參數拼接
        string paras = "";
        
int paraCount = paraName.Length;
        
if (paraValue.Length < paraCount)
            paraCount 
= paraValue.Length;
        
for (int i = 0; i < paraCount; i++)
        
{
            paras 
+= "&" + paraName[i] + "=" + Server.UrlEncode(paraValue[i]);
        }

        
//顯示格式
        string format = "";
        
switch (viewFormat)
        
{
            
case 0:
                format 
= "&rc:toolbar=true&rc:parameters=true&rc:Zoom=Page%20Width";
                
break;
            
case 1:
                format 
= "&rc:toolbar=true&rc:parameters=false&rc:Zoom=Page%20Width";
                
break;
            
case 2:
                format 
= "&rc:toolbar=false&rc:parameters=false&rc:Zoom=Page%20Width";
                
break;
            
default:
                format 
= "&rc:toolbar=true&rc:parameters=true&rc:Zoom=Page%20Width";
                
break;
        }

        
//組織最終報表URL
        string reportUrl = reportingServerUrlBase + reportCode + paras + format;

        
return reportUrl;
    }

 

/// <summary>
    
/// 打開報表窗口
    
/// </summary>
    
/// <param name="reportCode">報表代碼,如rp_pr_qa_001</param>
    
/// <param name="paraName">參數名稱數組,與參數值數組對應</param>
    
/// <param name="paraValue">參數值數組,與參數名稱數組對於</param>
    
/// <param name="viewFormat">顯示格式代碼,暫時只有兩種,0:顯示title,1:不顯示title</param>

    protected void PopUpReport(string reportCode, string[] paraName, string[] paraValue, int viewFormat)
    
{
        
//獲得報表地址
        string reportUrl = GetReportUrl(reportCode, paraName, paraValue, viewFormat);

        
//彈出報表窗口
        StringBuilder MyBuilder = new StringBuilder();
        MyBuilder.Append(
"<script language='javascript'>");
        MyBuilder.Append(
"{open('" + reportUrl + "','aa','width='+screen.width+' height='+screen.height+' top=0 left=0 toolbar=no menubar=no resizable=yes status=yes');}");
        MyBuilder.Append(
"</script>");
        
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "open", MyBuilder.ToString());
    }

調用事例
1.在頁面中某個區域直接顯示

合同號<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>    
<asp:Button ID="btnViewReportInPage" runat="server" Text="頁內顯示報表" OnClick="btnViewReportInPage_Click" />
<asp:Button ID="btnViewReportOutPage" runat="server" Text="彈出頁顯示報表" OnClick="btnViewReportOutPage_Click" />
<br />
<iframe id="RptFrame" runat="server" width="100%" height="500"></iframe>

 

protected void btnViewReportInPage_Click(object sender, EventArgs e)
 
{
        
string reportCode = "rp_pr_qa_136";
        
string[] paraName = new string[] {"ContractNoSys"};
       
string[] paraValue = new string[1]; 
        paraValue[
0= TextBox1.Text;
       ReportURL 
= GetReportUrl(reportCode, paraName, paraValue, 1);
       RptFrame.Attributes.Add(
"src", ReportURL);       
   }

2.點擊某個按鈕觸發後彈出新窗口顯示

protected void btnViewReportOutPage_Click(object sender, EventArgs e)
   
{
       
string reportCode = "rp_pr_qa_136";
       
string[] paraName = new string[] "ContractNoSys" };
       
string[] paraValue = new string[1];
       paraValue[
0= TextBox1.Text;
        PopUpReport(reportCode, paraName, paraValue, 
1);
   }



 

發佈了36 篇原創文章 · 獲贊 0 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章