使用ReportService2010.asmx以编程方式从sharepoint导出SSRS报告

时间:2013-01-23 11:56:24

标签: excel sharepoint reporting-services ssrs-2008

我必须以编程方式将ssrs报告导出到excel,添加服务引用

的http:/siteurl/_vti_bin/ReportServer/ReportService2010.asmx

的http:/siteurl/_vti_bin/ReportServer/ReportExecution2005.asmx

有人可以提供有效的博客

1 个答案:

答案 0 :(得分:10)

我这样做是为了我目前使用VS 2012 .NET 4.5进行PDF报告自动化报告的工作。

一个。为了便于使用,每次编译自己的代理类比引用Web服务更容易,因为您可能忘记了服务名称。

从Visual Studio命令提示符:

    wsdl /language:CS /n:"Microsoft.SqlServer.ReportingServices2010" http://<Server Name>/reportserver/reportservice2010.asmx?wsdl

参考:http://msdn.microsoft.com/en-us/library/ms155134(v=sql.105).aspx (只有当你想获取信息并执行时,你需要服务:ReportExecution2005和ReportService2010。如果你只想渲染你只需要ReportExecution2005)

B中。创建代理类后,将其放入库项目中以便重用IMHO。如果你有多个环境,可以构建一些包装类来编写和配置文件中的一些服务器,如果你有多个环境,你可能想要引用它们。

℃。编写一些引用库的代码,并在C#中构建第一次报告:

    using System;
    using System.IO;
    using System.Web.Services.Protocols;
    using myNamespace.MyReferenceName;  // YOUR PROXY PROJECT 

    class Sample
    {
    static void Main(string[] args)
    {
        ReportExecutionService rs = new ReportExecutionService();
        rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
        rs.Url = "http://myserver/reportserver/ReportExecution2005.asmx";

        // Render arguments
        byte[] result = null;
        string reportPath = "/AdventureWorks Sample Reports/Employee Sales Summary";
        string format = "MHTML";
        string historyID = null;
        string devInfo = @"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";

        // Prepare report parameter.
        ParameterValue[] parameters = new ParameterValue[3];
        parameters[0] = new ParameterValue();
        parameters[0].Name = "EmpID";
        parameters[0].Value = "288";
        parameters[1] = new ParameterValue();
        parameters[1].Name = "ReportMonth";
        parameters[1].Value = "6"; // June
        parameters[2] = new ParameterValue();
        parameters[2].Name = "ReportYear";
        parameters[2].Value = "2004";

        DataSourceCredentials[] credentials = null;
        string showHideToggle = null;
        string encoding;
        string mimeType;
        string extension;
        Warning[] warnings = null;
        ParameterValue[] reportHistoryParameters = null;
        string[] streamIDs = null;

        ExecutionInfo execInfo = new ExecutionInfo();
        ExecutionHeader execHeader = new ExecutionHeader();

        rs.ExecutionHeaderValue = execHeader;

        execInfo = rs.LoadReport(reportPath, historyID);

        rs.SetExecutionParameters(parameters, "en-us"); 
        String SessionId = rs.ExecutionHeaderValue.ExecutionID;

        Console.WriteLine("SessionID: {0}", rs.ExecutionHeaderValue.ExecutionID);


        try
        {
            result = rs.Render(format, devInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);

            execInfo = rs.GetExecutionInfo();

            Console.WriteLine("Execution date and time: {0}", execInfo.ExecutionDateTime);


        }
        catch (SoapException e)
        {
            Console.WriteLine(e.Detail.OuterXml);
        }
        // Write the contents of the report to an MHTML file.
        try
        {
            FileStream stream = File.Create("report.mht", result.Length);
            Console.WriteLine("File created.");
            stream.Write(result, 0, result.Length);
            Console.WriteLine("Result written to the file.");
            stream.Close();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }

    }
    }

从这里采取:http://msdn.microsoft.com/en-us/library/reportexecution2005.reportexecutionservice.render(v=sql.105).aspx

d。 (可选):您可能希望在SSRS托管服务器上刷新WebService。默认情况下,它每12小时回收一次,从而使第一个报告在缓慢后呈现。您可以通过在http://(servername)/ ReportServer上每10个小时左右对服务端点进行一次网络搜索来刷新这些服务。我使用一个名为Visual Cron的工具可以设置自动化任务,您也可以尝试更改SSRS服务配置,创建自己的保持活动服务等。基本上您只需要更改设置或者与之交谈以保持它打开了。