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

Save SSRS Report as PDF in C# programmatically

我已通读多篇有关此问题的文章,但它们都以无法正常工作告终,或者在 vb.net 中。

我目前拥有的:

报告通过 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:\temp”中,名称为 OrderID-1。我正在使用 C#

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

using ReportTestings;

和网络参考 URL:

http://Server/ReportServer_Name/ReportExecution2005.asmx

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

因此,基于以上所有这些信息,有人可以为我指明正确的方向或提供部分代码示例,感谢所有阅读本文的人 post 或帮助

使用这段代码我得到了这个错误:(+ 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);
    }

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

如果您需要呈现报告,您应该使用 ReportExecution2005 网络服务。

要开始,您应该看一下 Render 方法。


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

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

编辑:

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

解决它

我推荐一个我在 Crafted For Everyone 找到的非常简单的实现。作者展示了如何向 SSRS 服务器添加服务引用,然后提供了我在第一次尝试时就成功的示例代码。

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