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

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

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

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

从 Windows 窗体启动 Internet Explorer

可以使用 Process 类访问正在计算机上运行的进程。对于启动、停止、控制和监视应用程序等任务,Process 类是一个很有用的 Microsoft .NET Framework 构造。若要查看报表服务器数据库中的特定报表,可以启动 IExplore 进程,同时将 URL 传递给报表。以下代码示例可用于在用户单击 Windows 窗体上的某个按钮时,启动 Microsoft Internet Explorer 并传递特定的报表 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);
   }
}

有关报表服务器 URL 访问字符串的特定语法的详细信息,请参阅 URL 访问语法

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

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

将 WebBrowser 控件添加到 Windows 窗体

  1. 在 Microsoft Visual C# 或 Microsoft Visual Basic 中创建一个新的 Windows 应用程序。

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

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

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

    名为 webBrowser1 的 WebBrowser控件被添加到窗体中。

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

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