65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 C# 编程方式生成 SQL Server Reporting Services 2005 的 PDF 报告

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.52/5 (19投票s)

2006年9月12日

viewsIcon

505725

一篇关于如何使用 C# 编程方式生成 SQL Server Reporting Services 2005 的 PDF 报告的文章。

引言

SQL Server Reporting Services (SSRS) 2005 是微软最新的报告技术。本文介绍了一种使用 SQL Server Reporting Services 2005 暴露的 Web 服务,以 C# 编程方式创建 PDF 报告的方法。

使用代码

首先创建一个接受布尔值作为其输入参数的报告,并将其部署到报告服务器。下面的代码解释了如何使用 C# 以编程方式将报告渲染为 PDF 格式。

  • 步骤 1:创建并部署报告。
  • 步骤 2:为 Reporting Services 2005 暴露的 Web 服务添加 Web 引用,即 ReportExecution2005 和 ReportService2005。
  • 步骤 3:声明以下变量
    private rs2005.ReportingService2005 rs;
    private rsExecService.ReportExecutionService rsExec;
  • 步骤 4:初始化 Web 服务并设置 Web 服务的身份验证。
    // Create a new proxy to the web service
    rs = new rs2005.ReportingService2005();
    rsExec = new rsExecService.ReportExecutionService();
    
    // Authenticate to the Web service using Windows credentials
    rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
    rsExec.Credentials = System.Net.CredentialCache.DefaultCredentials;
    
    // Assign the URL of the Web service
    rs.Url = "http://ICCW1023/ReportServer" + 
             "$REPORTSERVER2005/ReportService2005.asmx";
    rsExec.Url = "http://ICCW1023/ReportServer" + 
                 "$REPORTSERVER2005/ReportExecution2005.asmx";
  • 步骤 5:编写代码以 PDF 格式渲染报告。
    // Prepare Render arguments
    string historyID = null;
    string deviceInfo = null;
    string format = "PDF";
    Byte[] results;
    string encoding = String.Empty;
    string mimeType = String.Empty;
    string extension = String.Empty;
    rsExecService.Warning[] warnings = null;
    string[] streamIDs = null;
    
    // Default Path;
    string fileName = @"c:\samplereport.pdf";
    
    // Define variables needed for GetParameters() method
    // Get the report name
    string _reportName = @"/MyReports/Report";
    string _historyID = null;
    bool _forRendering = false;
    ParameterValue[] _values = null;
    DataSourceCredentials[] _credentials = null;
    ReportParameter[] _parameters = null;
    
    try
    {
      // Get if any parameters needed.
      _parameters = rs.GetReportParameters(_report, _historyID, 
                    _forRendering, _values, _credentials);
    
      // Load the selected report.
      rsExecService.ExecutionInfo ei = 
            rsExec.LoadReport(_reportName, historyID);
    
      // Prepare report parameter.
      // Set the parameters for the report needed.
      rsExecService.ParameterValue[] parameters = 
             new rsExecService.ParameterValue[1];
    
      // Place to include the parameter.
      if (_parameters.Length > 0 )
      {
        parameters[0] = new rsExecService.ParameterValue();
        parameters[0].Label = "verzamelgroepAP";
        parameters[0].Name = "verzamelgroepAP";
        parameters[0].Value = "true";
      }
      rsExec.SetExecutionParameters(parameters, "en-us");
      results = rsExec.Render(format, deviceInfo, 
                out extension, out encoding,
                out mimeType, out warnings, out streamIDs);
    
      // Create a file stream and write the report to it
      using (FileStream stream = File.OpenWrite(fileName))
      {
        stream.Write(results, 0, results.Length);
      }
    }
    catch (Exception ex)
    {
      MessageBox.Show( ex.Message);
    }
© . All rights reserved.