本文介绍了以编程方式在 C# 中将 SSRS 报告另存为 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've read through multiple articles regarding this issue however they have all ended up with it not working, or are in vb.net.

我目前拥有的:

报告通过 URL 访问,该 URL 将它们呈现为 PDF 并在用户单击按钮时将它们保存在下载文件夹中,这些报告具有通用名称,例如 OrderReport、OrderReport(1)... 等等.

The reports are accessed via a URL which renders them as a PDF and saves them in the downloads folder when the user clicks on a button, these are given generic names such as OrderReport, OrderReport(1)... and so on.

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:emp".我正在使用 C#

so for example I would like to save this report in a temporary folder for now "C:emp" with the name OrderID-1. I am using C#

我已将 ServiceReference 添加到我正在使用的名为 ReportTestings 的项目中,因此参考是

I have added in a ServiceReference into the Project i am using called ReportTestings so the reference is

using ReportTestings;

和网络参考网址:

http://Server/ReportServer_Name/ReportExecution2005.asmx

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

(removed the actual names for security reasons)

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

so based on all of this information above could someone point me in the right direction or give an example part of code, Thankyou for all that read this post or help

使用此代码我收到此错误:(+ e

using this code i get this error :(+ 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) 用于管理报表服务器对象.

The webservice URL you are using (ReportService2012) is for managing the report server objects.

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

If you need to render reports, you should be using the ReportExecution2005 webservice.

要开始,您应该查看 Render 方法.

To get started, you should take a look at the Render method.

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

To specify credentials you can add the folowing line (I'm the same variable name used in your link: RS2005):

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

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

Your access denied error occurs when your application try to save the file with your web application, so you should use an absolute path or resolve it using Server.MapPath

这篇关于以编程方式在 C# 中将 SSRS 报告另存为 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 18:34