在 Windows 应用程序中使用 URL 访问

尽管对报表服务器的 URL 访问已针对 Web 环境进行优化,但也可以使用 URL 访问将 Reporting Services 报表嵌入到 Microsoft Windows 应用程序中。 但是,涉及 Windows 窗体的 URL 访问仍要求使用 Web 浏览器技术。 可以将以下集成方案与 URL 访问和 Windows 窗体配合使用:

  • 通过以编程方式启动 Web 浏览器,显示来自 Windows 窗体应用程序的报表。

  • WebBrowser使用 Windows 窗体上的控件显示报表。

从 Windows 窗体启动 Internet Explorer

可以使用 Process 该类访问计算机上运行的进程。 此类 Process 是一个有用的Microsoft .NET Framework 构造,用于启动、停止、控制和监视应用程序。 若要查看报表服务器数据库中的特定报表,可以启动 IExplore 过程,并将 URL 传递到报表。 以下代码示例可用于开始Microsoft Internet Explorer,并在用户单击 Windows 窗体上的按钮时传递特定的报表 URL。

Private Sub viewReportButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles viewReportButton.Click  
   ' Build the URL access string based on values supplied by a user  
   Dim url As String = serverUrlTextBox.Text + "?" & reportPathTextBox.Text & _  
   "&rs:Command=Render" & "&rs:Format=HTML4.0"  
  
   ' If the user does not select the toolbar check box,  
   ' turn the toolbar off in the HTML Viewer  
   If toolbarCheckBox.Checked = False Then  
      url += "&rc:Toolbar=False"  
   End If  
   ' load report in the Web browser  
   Try  
      System.Diagnostics.Process.Start("IExplore", url)  
   Catch  
      MessageBox.Show("The system could not start the specified report using Internet Explorer.", _  
      "An error has occurred", MessageBoxButtons.OK, MessageBoxIcon.Error)  
   End Try  
End Sub 'viewReportButton_Click  
// Sample click event for a Button control on a Windows Form  
private void viewReportButton_Click(object sender, System.EventArgs e)  
{  
   // Build the URL access string based on values supplied by a user  
   string url = serverUrlTextBox.Text + "?" + reportPathTextBox.Text +  
      "&rs:Command=Render" + "&rs:Format=HTML4.0";  
  
   // If the user does not check the toolbar check box,  
   // turn the toolbar off in the HTML Viewer  
   if (toolbarCheckBox.Checked == false)  
      url += "&rc:Toolbar=False";  
  
   // load report in the Web browser  
   try  
   {  
      System.Diagnostics.Process.Start("IExplore", url);  
   }  
  
   catch (Exception)  
   {  
      MessageBox.Show(  
         "The system could not open the specified report using Internet Explorer.",   
         "An error has occurred", MessageBoxButtons.OK, MessageBoxIcon.Error);  
   }  
}  

在 Windows 窗体上嵌入浏览器控件

如果不想在外部 Web 浏览器中查看报表,则可以使用 WebBrowser 控件无缝嵌入 Web 浏览器作为 Windows 窗体的一部分。

将 WebBrowser 控件添加到 Windows 窗体
  1. 在 Microsoft Visual C# 或 Microsoft Visual Basic 中创建新的 Windows 应用程序。

  2. “工具箱”对话框中找到该WebBrowser控件。

    如果 工具箱 不可见,可以通过单击 “视图 ”菜单项并选择 “工具箱”来访问它。

  3. WebBrowser控件拖到 Windows 窗体的设计图面上。

    WebBrowser名为 webBrowser1 的控件将添加到窗体

通过调用控件的 Navigate 方法将WebBrowser控件定向到 URL。 可以在运行时为控件 WebBrowser 分配特定的 URL 访问字符串,如以下示例所示。

Dim url As String = "https://localhost/reportserver?/" & _  
                    "AdventureWorks2012 Sample Reports/" & _  
                    "Company Sales&rs:Command=Render"  
WebBrowser1.Navigate(url)  
string url = "https://localhost/reportserver?/" +  
             "AdventureWorks2012 Sample Reports/" +  
             "Company Sales&rs:Command=Render";  
webBrowser1.Navigate(url);  

另请参阅

将 Reporting Services 集成到应用程序中
使用 URL 访问集成 Reporting Services
使用 SOAP 集成 Reporting Services
使用 ReportViewer 控件集成 Reporting Services
URL 访问 (SSRS)