2012-08-30 85 views
4

我有一個控制檯應用程序需要連接到SQL Server Express 2012上的SQL Reporting Services。 所有導出邏輯應該在單獨的dll中實現,並且路徑應該是動態的(我已經循環了各種服務器/報告的各種設置,並將它們逐個導出到Excel中)。以編程方式從SQL 2012 Reporting Services導出報表

我試圖按照這些教程:

http://www.aspose.com/docs/display/wordsreportingservices/Rendering+Reports+Programmatically

http://blogs.msdn.com/b/selvar/archive/2010/12/13/accessing-the-reportexecutionservice-render-method-when-reporting-service-2008-r2-is-in-forms-based-authentication.aspx

基本上,我添加的Web引用:

http://localhost:80/ReportServer_SQLEXPRESS12/ReportExecution2005.asmx 

http://localhost:80/ReportServer_SQLEXPRESS12/ReportService2010.asmx 

我的dll。看起來不錯,除了那些令人討厭的app.config設置(我將不得不稍後動態調整它們)。

然後我試圖做作爲例子:

// Create Web service proxies. 
ReportingService2010.ReportingService2010 rs = new ReportingService2010.ReportingService2010(); 
ReportExecutionService.ReportExecutionService rsExec = new ReportExecutionService.ReportExecutionService(); 

,並得到了一些麻煩:

Error 76 The type or namespace name 'ReportExecutionService' does not exist in the namespace 'MyDllNamespace.ReportExecutionService' (are you missing an assembly reference?)  

Error 74 The type or namespace name 'ReportingService2010' does not exist in the namespace 'MyDllNamespace.ReportingService2010' (are you missing an assembly reference?) 

現在在哪兒我去下一個,我該如何使用Reporting Services API,如果我甚至不能創建代理對象?或者我應該更好地使用ServerReport類形式的Winforms ReportViewer而不是這些Web引用?

即使其中一個Microsoft示例在控制檯應用程序中使用ReportViewer,但在控制檯應用程序中導入Winforms似乎有些尷尬。

+0

弄來使用溶液(代碼相關部分的提取物)*** http://DEVrpsw01/reportserver_reporting/reportexecution2005.asmx?wsdl ***? – Kiquenet

+0

@Kiquenet:我最終使用'Microsoft.Reporting.WinForms.ServerReport'結合了接受答案中由tschmit007給出的代碼片斷。 – JustAMartin

回答

9

我希望以下將幫助

  using (ZUtilities.SSRS.Report report = new ZUtilities.SSRS.Report { 
       ReportServerPath = VParameter.GetValue("SSRS_WebServiceUrl", _repo.Parameters).ToString(), 
       Format = rformat, 
       ReportPath = "some_path_on_ssrs_server" 
      }) { 
       report.Params.Add("Id", id.ToString()); 
       report.Credentials = nwc; 
       MemoryStream ms = new MemoryStream(); 
       report.Render().CopyTo(ms); 
       FileContentResult fsr = new FileContentResult(ms.ToArray(), rctype); 
       fsr.FileDownloadName = String.Format("EPV-{0}-{1:yyyyMMdd}.{2}", epv.ExternalReference, DateTime.Now, fext); 
       ms.Close(); 
       return fsr; 
      } 

以下的(要小心,以流管理)

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Net; 
using System.IO; 

namespace ZUtilities.SSRS { 
    public enum ReportFormats { 
     Html = 1, 
     MHtml, 
     Pdf, 
     Xlsx, 
     Docx 
    } 

    public class ReportFormat { 
     static ReportFormat() { 
      Html = new ReportFormat { Code = ReportFormats.Html, Instruction = "HTML4.0" }; 
      MHtml = new ReportFormat { Code = ReportFormats.MHtml, Instruction = "MHTML" }; 
      Pdf = new ReportFormat { Code = ReportFormats.Pdf, Instruction = "PDF" }; 
      Xlsx = new ReportFormat { Code = ReportFormats.Xlsx, Instruction = "EXCELOPENXML" }; 
      Docx = new ReportFormat { Code = ReportFormats.Docx, Instruction = "WORDOPENXML" }; 
     } 

     private ReportFormat() { 
     } 

     public ReportFormats Code { get; set; } 
     public String Instruction { get; set; } 

     public static ReportFormat Html { get; private set; } 
     public static ReportFormat MHtml { get; private set; } 
     public static ReportFormat Pdf { get; private set; } 
     public static ReportFormat Xlsx { get; private set; } 
     public static ReportFormat Docx { get; private set; } 

     public static ReportFormat ByCode(ReportFormats code) { 
      switch (code) { 
       case ReportFormats.Html: return Html; 
       case ReportFormats.MHtml: return Html; //<<====================== 
       case ReportFormats.Pdf: return Pdf; 
       case ReportFormats.Xlsx: return Xlsx; 
       case ReportFormats.Docx: return Docx; 
       default : return null; 
      } 
     } 
    } 

    public class Report : IDisposable { 
     private HttpWebRequest _httpWReq; 
     private WebResponse _httpWResp; 

     public Report() { 
      _httpWReq = null; 
      _httpWResp = null; 
      Format = ReportFormats.Html; 
      Params = new Dictionary<String, String>(); 
     } 

     public Dictionary<String, String> Params { get; set; } 

     public String ReportServerPath { get; set; } 
     public String ReportPath { get; set; } 
     public ReportFormats Format { get; set; } 
     public NetworkCredential Credentials { get; set; } 

     //public String PostData { get { return String.Format("rs:Command=Render&rs:Format={0}", ReportFormat.ByCode(Format).Instruction); } } 
     public String PostData { get { 
      StringBuilder sb = new StringBuilder(1024); 
      sb.AppendFormat("rs:Command=Render&rs:Format={0}", ReportFormat.ByCode(Format).Instruction); 
      if (Format == ReportFormats.Html) { 
       sb.Append("&rc:Toolbar=false"); 
      } 
      foreach (var kv in Params) { 
       sb.AppendFormat("&{0}={1}", kv.Key, kv.Value); 
      } 

      return sb.ToString(); 
     } } 

     public String ReportFullPath { get { return ReportServerPath + "?/" + ReportPath; } } 

     public Stream Render() { 
      _httpWReq = (HttpWebRequest)HttpWebRequest.Create(ReportFullPath); 
      _httpWReq.Method = "POST"; 
      if (Credentials != null) 
       _httpWReq.Credentials = Credentials; 

      byte[] byteArray = Encoding.UTF8.GetBytes(PostData); 
      _httpWReq.ContentType = "application/x-www-form-urlencoded"; 
      _httpWReq.ContentLength = byteArray.Length; 
      Stream dataStream = _httpWReq.GetRequestStream(); 
      dataStream.Write(byteArray, 0, byteArray.Length); 
      dataStream.Close(); 

      if (_httpWResp != null) 
       _httpWResp.Close(); 
          _httpWResp = _httpWReq.GetResponse();    

      return _httpWResp.GetResponseStream(); 
     } 

     public void RenderTo(String fileName) {    
      Stream receiveStream = Render(); 
      Stream ds = File.Open(fileName, FileMode.Create); 
      receiveStream.CopyTo(ds); 
      ds.Close(); 
      receiveStream.Close();    
     } 

     public void Dispose() { 
      if (_httpWResp != null) { 
       _httpWResp.Close(); 
       _httpWResp = null; 
      } 

      if (_httpWReq != null) { 
       _httpWReq = null; 
      } 
     } 
    } 
} 
+0

嗨,我想要使用上面的代碼,保存時我得到一個損壞的PDF或Excel文件。任何想法爲什麼?謝謝 – Bill

+0

你使用Render還是RenderTo。如果使用Render,則可能是返回的流缺少close()。 – tschmit007

+0

關閉就在那裏。但仍然是同樣的問題。 – Bill

相關問題