动态存储和发送SSRS报告为PDF与c#
本文关键字:PDF 报告 SSRS 存储 动态 | 更新日期: 2023-09-27 18:12:27
我在SSRS中有一个报告,它将SalesRepCode
和Email
作为参数来生成PDF收据。如果我使用报表查看器,它就会像它应该的那样工作。
使用c#,我想为每个存在的SalesRep自动生成PDF,一旦PDF呈现,我想将其存储在一个文件夹中,然后将其作为电子邮件附件发送。
我看过ReportingService2005
类的MSDN文档,但这是指2005年版本,我正在使用SSRS 2012,我仍然没有找到与我使用的版本相关的东西。
是否有合适的方法来实现这一点?
可以使用ReportExecutionService的Render
方法,没有任何问题。您需要向ReportExecution2005添加一个服务引用,它是报表服务器的执行端点。
下面的例子取自msdn,并做了一些小改动。要查看原始示例,请查看msdn示例。使用时,请注意使用PDF
作为格式,同时注意报告路径,它应该是以/
开头,以报告名称结尾,而不是.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);
我已经使用了ReportingService2005 Web Service与SQL 2012,它工作得很好。
简而言之-你添加引用到Web Service,绑定参数,调用Render()与PDF和保存字节流作为一个本地文件。
有两个令人困惑的Web服务(报告服务&报告执行服务)
我认为你可以使用报告执行服务。
首先要检查的是,您可以在SSRS页面上看到.asmx文件(http://...ReportExecution2005.asmx)