简体   繁体   中英

Save SSRS Report as PDF in C# programmatically

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

What I currently have:

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"

What I am trying to acheive:

  • I would like to use C# to fetch this report if possible, and then specify a name for the PDF file and save it in the correct location.

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

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

using ReportTestings;

and the Web Reference URL:

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

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

code:

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

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

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

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


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

EDIT:

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

I recommend an amazingly simple implementation that I found at Crafted For Everyone . The author shows how to add a Service Reference to the SSRS server, and then provides sample code that worked for me on the first try.

It saves the report to a local file, but it is easy to send the report in an email (not included in sample.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM