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

以编程方式在C#中将SSRS报告另存为PDF,c#,reporting-services,report,C#,Reporting Services,Report,我已经阅读了关于这个问题的多篇文章,但是它们都以它不起作用而告终,或者都在vb.net中 我目前拥有的: 通过URL访问报告,该URL将报告呈现为PDF格式,并在用户单击按钮时将其保存在下载文件夹中,这些报告具有通用名称,如OrderReport、OrderReport(1)。。。等等 var orderNum = 1; "http://Server/ReportServer_Name/Pages/ReportViewer.aspx?%2fOrderReport&rs:Command=

我已经阅读了关于这个问题的多篇文章,但是它们都以它不起作用而告终,或者都在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#

我已经在我正在使用的项目中添加了一个名为ReportTestings的ServiceReference,因此引用是

using ReportTestings;
和Web参考URL:

(出于安全原因,删除了实际名称)

因此,基于以上所有信息,有人能给我指出正确的方向或给出代码的示例部分吗?谢谢所有阅读本文或提供帮助的人

使用这段代码我得到了这个错误:(+e

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

ReportExecutionService rs=新的ReportExecutionService();
rs.凭证=新的网络凭证(“用户名”、“密码”、“域”);
rs.Url=”http://Server/ReportServer_Name/reportexecution2005.asmx";
//渲染参数
字节[]结果=空;
字符串reportPath=“/Invoice”;
string format=“PDF”;
字符串historyID=null;
字符串devInfo=@“False”;
//准备报表参数。
ParameterValue[]参数=新参数值[3];
参数[0]=新参数值();
参数[0]。Name=“InvoiceID”;
参数[0]。Value=“2”;
DataSourceCredentials[]凭证=null;
字符串showHideToggle=null;
字符串编码;
字符串模拟类型;
字符串扩展;
警告[]警告=null;
ParameterValue[]reportHistoryParameters=null;
字符串[]streamIDs=null;
ExecutionInfo ExecutionInfo=新的ExecutionInfo();
ExecutionHeader ExecutionHeader=新的ExecutionHeader();
rs.ExecutionHeaderValue=ExecutionHeader;
execInfo=rs.LoadReport(reportPath,historyID);
rs.SetExecutionParameters(参数“en-us”);
字符串SessionId=rs.ExecutionHeaderValue.ExecutionID;
WriteLine(“SessionID:{0}”,rs.ExecutionHeaderValue.ExecutionID);
尝试
{
结果=rs.Render(格式、devInfo、输出扩展、输出编码、输出mimeType、输出警告、输出streamid);
execInfo=rs.GetExecutionInfo();
WriteLine(“执行日期和时间:{0}”,execInfo.ExecutionDateTime);
}
捕获(SOAPE例外)
{
Console.WriteLine(e.Detail.OuterXml);
}
//将报告内容写入MHTML文件。
尝试
{
FileStream stream=File.Create(“report1one.pdf”,result.Length);
WriteLine(“文件已创建”);
stream.Write(result,0,result.Length);
WriteLine(“结果写入文件”);
stream.Close();
}
捕获(例外e)
{
控制台写入线(e.Message);
}

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

如果需要呈现报告,则应使用webservice

要开始,您应该看看这个方法


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

编辑:


当您的应用程序尝试用web应用程序保存文件时,会出现拒绝访问错误,因此您应该使用绝对路径或使用

解决此问题。我推荐一个在中找到的非常简单的实现。作者介绍了如何将服务引用添加到SSRS服务器,然后提供了适用于我的示例代码第一次尝试


它将报告保存到本地文件中,但通过电子邮件发送报告很容易(未包含在示例中。

我一直在尝试此操作,但在尝试将报告保存到任何位置时,我会一直拒绝访问。请看上面的示例,我现在将添加该示例,如果您能够克服拒绝访问错误,这将对我提供的链接中的示例有很大帮助()很简单,你看过了吗?好的,那么请编辑你的帖子,在我使用的代码中添加详细的错误信息,并在你包含的关于凭据的部分中添加错误信息,但不幸的是,仍然是相同的错误信息。你能试着使用绝对路径,看看它是否有效吗?如果你使用的是相对路径,你应该请使用类似于
Server.MapPath
的方法来解决此问题。
    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);
    }
RS2005.Credentials = new System.Net.NetworkCredential("username", "password", "domain");