2015-05-07 138 views
4

我已阅读了关于此问题的多篇文章,但它们都以它不工作或以vb.net结尾。以编程方式将C#中的SSRS报告另存为

我目前有:

该报告通过这使得他们作为一个PDF,当用户点击一个按钮,将它们保存在下载文件夹中的网址访问,这些被赋予通用名称,如OrderReport,OrderReport(1)...等等。

var orderNum = 1; 

"http://Server/ReportServer_Name/Pages/ReportViewer.aspx?%2fOrderReport&rs:Command=Render&OrderID=" + orderNum + "&rs:ClearSession=true&rs:Format=PDF" 

什么我试图acheive:

  • 我想用C#如果可能,本报告取,然后为PDF文件指定一个名称,并将其保存在正确的位置。

因此,例如,我想这份报告在临时文件夹中保存现在的“C:\ TEMP”的名称订单ID-1。我使用C#

我已经在服务引用添加到我使用所谓ReportTestings所以引用

using ReportTestings; 

和Web引用URL项目:

http://Server/ReportServer_Name/ReportExecution2005.asmx

(去除实际名称为安全原因)

所以基于上述所有这些信息可能有人指向我正确的方向还是举个例子的代码的一部分,三江源所有看到这篇文章,或帮助

使用此代码我得到这个错误:(+ E

{"Access to the path 'C:\\Program Files (x86)\\IIS Express\\report1one.pdf' is denied."} System.Exception {System.UnauthorizedAccessException}) 

代码:

ReportExecutionService rs = new ReportExecutionService(); 
    rs.Credentials = new NetworkCredential("username", "password", "domain"); 
    rs.Url = "http://Server/ReportServer_Name/reportexecution2005.asmx"; 

    // Render arguments 
    byte[] result = null; 
    string reportPath = "/Invoice"; 
    string format = "PDF"; 
    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 = "InvoiceID"; 
    parameters[0].Value = "2"; 

    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("report1one.pdf", 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); 
    } 

回答

4

您正在使用的webservice URL(ReportService2012)用于管理报表服务器对象。

如果你需要渲染报告,你应该使用ReportExecution2005 webservice。

要开始,你应该看看Render方法。


要指定凭据,您可以添加如下因素线(我在你的链接中使用相同的变量名:RS2005):

RS2005.Credentials = new System.Net.NetworkCredential("username", "password", "domain"); 

编辑:

您的访问被拒绝当您的应用程序尝试使用您的Web应用程序保存文件时发生错误,因此您应该使用绝对路径或使用其解析Server.MapPath

+0

我一直在尝试这个,但当我试图将报告保存在任何地方时,我一直在访问被拒绝。看看上面的例子现在添加,如果你可以通过访问被拒绝的错误,这将有助于很多 – Crezzer7

+0

在我提供的链接(https://msdn.microsoft.com/en-us/library/reportexecution2005.reportexecutionservice.render.aspx)非常简单,你看过吗? –

+0

是的,仍然存在被拒绝的问题 – Crezzer7

相关问题