簡體   English   中英

以編程方式在 C# 中將 SSRS 報告另存為 PDF

[英]Save SSRS Report as PDF in C# programmatically

我已經閱讀了多篇關於這個問題的文章,但是它們最終都無法正常工作,或者在 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 文件指定一個名稱並將其保存在正確的位置。

因此,例如,我想在一個臨時文件夾中保存該報告現在“C:\\ TEMP”名為訂單ID-1。 我正在使用 C#

我已將 ServiceReference 添加到我正在使用的名為 ReportTestings 的項目中,因此參考是

using ReportTestings;

和 Web 參考 URL:

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

您使用的 Web 服務 URL (ReportService2012 ) 用於管理報表服務器對象。

如果您需要呈現報告,您應該使用ReportExecution2005 網絡服務

首先,您應該查看Render方法。


要指定憑據,您可以添加以下行(我與您的鏈接中使用的變量名稱相同: RS2005 ):

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

編輯:

當您的應用程序嘗試使用您的 Web 應用程序保存文件時,您的訪問被拒絕錯誤發生,因此您應該使用絕對路徑或使用Server.MapPath解決它

我推薦一個非常簡單的實現,我在Crafted For Everyone 上找到了這個實現。 作者展示了如何向 SSRS 服務器添加服務引用,然后提供了在第一次嘗試時對我有用的示例代碼。

它將報告保存到本地文件,但很容易通過電子郵件發送報告(不包含在示例中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM