以编程方式在C#中将SSRS报告另存为PDF

我已经阅读了关于这个问题的多篇文章,但是他们最终都没有工作,或者是在vb.net中。

我现在有什么:

报告可通过URL访问,该URL将其呈现为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" 

我想要实现的目标:

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

例如,我想将此报告保存在临时文件夹中,现在名为OrderID-1的C:\ temp ”。 我正在使用C#

我在ServiceReference中添加了一个名为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 = @"False"; // 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); } 

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

如果需要呈现报告,则应使用ReportExecution2005 Web服务。

首先,您应该看一下Render方法。


要指定凭据,您可以添加以下行(我的链接中使用的变量名称是相同的: RS2005 ):

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

编辑:

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