C# 以编程方式从SQL 2012 Reporting Services导出报表

C# 以编程方式从SQL 2012 Reporting Services导出报表,c#,web-services,reporting-services,sql-server-2012,C#,Web Services,Reporting Services,Sql Server 2012,我有一个控制台应用程序,需要连接到SQL Server Express 2012上的SQL Reporting Services。 所有导出逻辑都应该在一个单独的dll中实现,路径应该是动态的(我循环了各种服务器/报表的各种设置,并将它们逐个导出到Excel) 我尝试遵循以下教程: 基本上,我添加了网络参考: http://localhost:80/ReportServer_SQLEXPRESS12/ReportExecution2005.asmx 及 到我的dll。到目前为止看起来不错,

我有一个控制台应用程序,需要连接到SQL Server Express 2012上的SQL Reporting Services。 所有导出逻辑都应该在一个单独的dll中实现,路径应该是动态的(我循环了各种服务器/报表的各种设置,并将它们逐个导出到Excel)

我尝试遵循以下教程:

基本上,我添加了网络参考:

http://localhost:80/ReportServer_SQLEXPRESS12/ReportExecution2005.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?或者我最好使用Winforms ReportViewer中的ServerReport类,而不是这些web引用?


甚至有一个Microsoft示例在控制台应用程序中使用ReportViewer,但在控制台应用程序中导入Winforms似乎有点尴尬。

我希望以下内容能有所帮助(代码相关部分的摘录)

具有以下内容(注意流管理)

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
Net系统;
使用System.IO;
名称空间ZUtilities.SSRS{
公共枚举报告格式{
Html=1,
MHtml,
Pdf,
Xlsx,
Docx
}
公共类报表格式{
静态报告格式(){
Html=newreportformat{Code=ReportFormats.Html,Instruction=“HTML4.0”};
MHtml=newreportformat{Code=ReportFormats.MHtml,指令=“MHtml”};
Pdf=newreportformat{Code=ReportFormats.Pdf,Instruction=“Pdf”};
Xlsx=newreportformat{Code=ReportFormats.Xlsx,Instruction=“EXCELOPENXML”};
Docx=newreportformat{Code=ReportFormats.Docx,Instruction=“WORDOPENXML”};
}
私有报表格式(){
}
公共报告格式代码{get;set;}
公共字符串指令{get;set;}
公共静态报表格式Html{get;private set;}
公共静态报告格式MHtml{get;private set;}
公共静态报表格式Pdf{get;private set;}
公共静态报表格式Xlsx{get;private set;}
公共静态报表格式Docx{get;private set;}
公共静态ReportFormat ByCode(ReportFormats代码){
开关(代码){
case ReportFormats.Html:返回Html;

case ReportFormats.MHtml:return Html;//您好,我正在尝试使用上面的代码,保存时得到的PDF或Excel文件已损坏。知道原因吗?感谢您使用Render或RenderTo。如果使用Render,可能缺少close()对于返回的流,存在关闭。但仍然存在相同的问题。关闭前可能会刷新?我还得到一个损坏的文件-但我没有使用FileContentResult,因此这可能是我的实现的问题。Adobe Reader无法打开“test.pdf”,因为它不是受支持的文件类型,或者因为文件已损坏(例如,它是作为电子邮件附件发送的,未正确解码)。您使用了解决方案吗?@Kiquenet:我最终使用了
Microsoft.Reporting.WinForms.ServerReport
以及tschmit007在接受的答案中给出的代码片段。
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?)
            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;
            }
        }
    }
}