Dynamically store and send SSRS reports as PDF with C#

浪子不回头ぞ 提交于 2019-12-24 01:42:11

问题


I have a report in SSRS that takes as a parameter a SalesRepCode and Email to generate a PDF receipt. It's working as it should be if I'm using the Report Viewer.

With C#, I'd like to automatically generate a PDF for each SalesRep that exists, once the PDF is rendered, I'd like to store it on a folder and then send it as an email attachment.

I have looked at the MSDN documentation of the ReportingService2005 Class, but this refers to the 2005 version, and I'm using SSRS 2012 and I still havent found a thing related to the version I'm using.

Is there a proper way of making this happen?


回答1:


You can use Render method of ReportExecutionService without any problem. You need to add a service reference to ReportExecution2005 which is the Execution Endpoint of report server.

Example

Below example has been taken from msdn and used by some small changes. To see the original example take a look at msdn example. When using, pay attention to use PDF as format and also about the report path it should be the path of your report starting by / and ending with report name without .rdl.

ReportExecutionService rs = new ReportExecutionService();
rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
rs.Url = "http://MyServer/reportserver/ReportExecution2005.asmx";
/* Render arguments */
byte[] result = null;
string reportPath = "/MyFolder/MyReport"; 
string format = "PDF";
string historyID = null;
string devInfo = @"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";          
/* Prepare report parameter.*/
ParameterValue[] parameters = new ParameterValue[1];
parameters[0] = new ParameterValue();
parameters[0].Name = "SomeParameter";
parameters[0].Value = "SomeValue";
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; 
/*Load and Render Report*/
execInfo = rs.LoadReport(reportPath, historyID);
rs.SetExecutionParameters(parameters, "en-us");
String SessionId = rs.ExecutionHeaderValue.ExecutionID;
result = rs.Render(format, devInfo, out extension, out encoding, 
    out mimeType, out warnings, out streamIDs);
execInfo = rs.GetExecutionInfo();
/*Save File*/
System.IO.File.WriteAllBytes(@"d:\report.pdf", result);



回答2:


I've used the ReportingService2005 Web Service with SQL 2012 and it works fine.

In a nutshell - You add the reference to the Web Service, Bind Params, Call Render() with PDF and save the ByteStream as a local file.

There are confusingly 2 Web Services (Report Service & Report Execution Service)

I think you can get away with just using the Report Execution Service.

First thing to check is that you can see the .asmx file on your SSRS Page (http://...ReportExecution2005.asmx)



来源:https://stackoverflow.com/questions/39980596/dynamically-store-and-send-ssrs-reports-as-pdf-with-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!