【问题标题】:Save SSRS Report as PDF in C# programmatically以编程方式在 C# 中将 SSRS 报告另存为 PDF
【发布时间】:2015-05-07 08:15:45
【问题描述】:

我已经阅读了有关此问题的多篇文章,但是它们都以它不起作用或在 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;

和网络参考网址:

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);
    }

【问题讨论】:

    标签: c# reporting-services report


    【解决方案1】:

    您使用的网络服务 URL (ReportService2012) 用于管理报表服务器对象。

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

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


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

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

    编辑:

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

    【讨论】:

    • 我一直在尝试这个,但是当我试图将报告保存在任何地方时,我一直被拒绝访问。看看上面的例子,现在添加,如果你能克服访问被拒绝的错误,这将有助于太可怕了
    • 我提供的链接中的示例 (msdn.microsoft.com/en-us/library/…) 非常简单,你看过吗?
    • 好的,请编辑您的帖子并添加错误详情
    • 添加到我正在使用的代码中并添加到您包含的有关凭据的部分中,但不幸的是,仍然是相同的错误消息
    • 你可以试试绝对路径,看看它是否有效?如果您使用的是相对路径,则应该使用 Server.MapPath 之类的东西来解决它。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-04
      • 2011-12-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多