/// <summary>
/// Handles frequently used functionalities in ReportViewer Controls to display SSRS reports locally.
/// </summary>
public class SSRSReport
{
private static String GetReportServerURL()
{
DataTable datatable = new DataTable();
//Execute the stored procedure to get the Report Server URL from the database.
DBConnect.FillDataTable("GetSSRSReportServerURL", datatable, null);
if (datatable == null || datatable.Rows.Count == 0)
return null;
else
return datatable.Rows[0]["PARAMETER_VALUE"].ToString().Trim();
}
/// <summary>
/// Open the SSRS report based on the name of the report specified.
/// </summary>
/// <param name="reportViewer">ReportViewer
/// object used to render the SSRS report on screen.</param>
/// <param name="reportPath">Name of the Report
/// (.rdl) data uploaded on the server.</param>
public static void DisplayReport(ReportViewer reportViewer, String reportPath)
{
try
{
reportViewer.ProcessingMode = ProcessingMode.Remote;
ServerReport serverreport = reportViewer.ServerReport;
ICredentials credentials = CredentialCache.DefaultCredentials;
ReportServerCredentials rscredentials = serverreport.ReportServerCredentials;
rscredentials.NetworkCredentials = credentials;
serverreport.ReportServerUrl = new Uri(GetReportServerURL());
serverreport.ReportPath = reportPath;
reportViewer.ShowParameterPrompts = false;
reportViewer.ShowPrintButton = true;
reportViewer.Refresh();
reportViewer.SetDisplayMode(DisplayMode.PrintLayout);
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// Open the SSRS report based on the name of the report and Report Parameters specified.
/// </summary>
/// <param name="reportViewer">ReportViewer
/// object used to render the SSRS report on screen.</param>
/// <param name="reportPath">Name of the Report
/// (.rdl) data uploaded on the server.</param>
/// <param name="reportParameterList">
/// List of Report parameters.</param>
public static void DisplayReport(ReportViewer reportViewer,
String reportPath, List<ReportParameter> reportParameterList)
{
try
{
reportViewer.ProcessingMode = ProcessingMode.Remote;
ServerReport serverreport = reportViewer.ServerReport;
ICredentials credentials = CredentialCache.DefaultCredentials;
ReportServerCredentials rscredentials = serverreport.ReportServerCredentials;
rscredentials.NetworkCredentials = credentials;
serverreport.ReportServerUrl = new Uri(GetReportServerURL());
serverreport.ReportPath = reportPath;
reportViewer.ShowParameterPrompts = false;
reportViewer.ShowPrintButton = true;
if (reportParameterList != null)
{
foreach (ReportParameter param in reportParameterList)
{
serverreport.SetParameters(param);
}
}
reportViewer.Refresh();
reportViewer.SetDisplayMode(DisplayMode.PrintLayout);
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// Convert the SSRS report on the local report viewer to an Attachment.
/// This can be used to attach the PDF to an email.
/// </summary>
/// <param name="reportViewer">ReportViewer control.</param>
/// <param name="fileName">Name of the PDF data.</param>
/// <returns>PDF File as an Attachment that
/// can be attached to an email using SMTPClient.</returns>
public static Attachment ConvertToPDFAttachment(ReportViewer reportViewer, String fileName)
{
try
{
byte[] data;
if (reportViewer.ServerReport != null)
data = reportViewer.ServerReport.Render("PDF");
else
data = reportViewer.LocalReport.Render("PDF");
Attachment att = new Attachment(new MemoryStream(data), fileName);
return att;
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// Saves the report from the local reportViewer as PDF.
/// To execute this method, the reportviewer needs to already contain the SSRS report.
/// </summary>
/// <param name="reportViewer">ReportViewer
/// control that displays the report.</param>
/// <param name="filePath">Path of the file
/// to which the report should be stored as PDF.</param>
/// <returns>True,if saved successfully ; False,otherwise.</returns>
public static Boolean SaveAsPDF(ReportViewer reportViewer, String filePath)
{
try
{
byte[] data;
if (reportViewer.ServerReport != null)
data = reportViewer.ServerReport.Render("PDF");
else
data = reportViewer.LocalReport.Render("PDF");
FileStream fs = new FileStream(filePath, FileMode.Create);
fs.Write(data, 0, data.Length);
fs.Close();
return true;
}
catch(Exception)
{
throw;
}
}
/// <summary>
/// Saves the report from the SSRS Report Server as PDF.
/// </summary>
/// <param name="reportViewer">ReportViewer
/// control that displays the report.</param>
/// <param name="filePath">Path of the file
/// to which the report should be stored as PDF.</param>
/// <param name="reportPath">Name of the Report
/// (.rdl) data uploaded on the server.</param>
/// <returns>True,if saved successfully ; False,otherwise.</returns>
public static Boolean SaveAsPDF(ReportViewer reportViewer, String filePath, String reportPath)
{
try
{
DisplayReport(reportViewer, reportPath);
byte[] data;
if (reportViewer.ServerReport != null)
data = reportViewer.ServerReport.Render("PDF");
else
data = reportViewer.LocalReport.Render("PDF");
FileStream fs = new FileStream(filePath, FileMode.Create);
fs.Write(data, 0, data.Length);
fs.Close();
return true;
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// Saves the report from the SSRS Report Server as PDF.
/// </summary>
/// <param name="reportViewer">ReportViewer
/// control that displays the report.</param>
/// <param name="filePath">Path of the file
/// to which the report should be stored as PDF.</param>
/// <param name="reportPath">Name of the Report
/// (.rdl) data uploaded on the server.</param>
/// <param name="reportParameterList">List of Report parameters.</param>
/// <returns>True,if saved successfully ; False,otherwise.</returns>
public static Boolean SaveAsPDF(ReportViewer reportViewer,
String filePath, String reportPath, List<ReportParameter> reportParameterList)
{
try
{
DisplayReport(reportViewer, reportPath,reportParameterList);
byte[] data;
if (reportViewer.ServerReport != null)
data = reportViewer.ServerReport.Render("PDF");
else
data = reportViewer.LocalReport.Render("PDF");
FileStream fs = new FileStream(filePath, FileMode.Create);
fs.Write(data, 0, data.Length);
fs.Close();
return true;
}
catch (Exception)
{
throw;
}
}
}
如何使用这个类:
public class SSRSReportViewer
{
private ReportViewer _reportViewer;
//Display Report with NO parameters.
SSRSReport.DisplayReport(_reportViewer, ReportPath);
//Display Report WITH Parameters.
List<ReportParameter> paramList = new List<ReportParameter>();
paramList.Add(new ReportParameter("param1", param1);
paramList.Add(new ReportParameter("param2", param2);
SSRSReport.DisplayReport(_reportViewer, ReportPath ,paramList);
//Convert the Report to PDF Attachment.
//This Attachment object can be used along with SMTPClient
//object to send the report as a PDF attachment with an email.
Attachment att = SSRSReport.ConvertToPDFAttachment(this._reportViewer, fileName);
//Save Report with NO parameters already displayed on the reportViewer as PDF.
SSRSReport.SaveAsPDF(_reportViewer, filePath);
//Save Report with NO Parameters as PDF without displaying it in the ReportViewer.
SSRSReport.SaveAsPDF(_reportViewer, filePath, ReportPath);
//Save the Report WITH parameters as PDF.
SSRSReport.SaveAsPDF(_reportViewer, filePath, ReportPath, paramList);
}
3 回答
我相信2005年之后的所有SSRS版本都有一个SOAP Web服务,可以访问并提供您所需的内容 . C#应该能够读取它(或者如果你的代码更快,你可以渲染客户端) . GetDataSourceContents是要查找的元素 . Here's 2008年的文件 .
您可以在2008 here找到Microsoft的完整 endpoints 规范 . 应该有一个带有reportservice2008.asmx的目录(或者如果您认为this讨论,则为... 2006 for SharePoint) . Here's有关如何将服务引用添加到Visual Studio的文档 .
这是我觉得它可能适合你的一段代码
如何使用这个类:
Source
已经说得好,更简单的方法是使用webservice:
案例1:您要列出所有报告
使用"CatalogItem()"服务
以任何你想要的方式显示并让用户选择
案例2:您确切地知道要启动或用户选择哪个报告(通过单击元素)来显示一个
然后是简单的电话:
http://YOUR_REPORTING_SERVER_URL?/PATH_TO_SPECIFIC_REPORT/REPORT_NAME&rs:Command=Render&rs:Format=EXCEL&ADDITIONNAL_PARAMETER1=VALUE
注意:rs:格式在这里配置为导出Excel(您也可以定义pdf等...) . 让报告服务为您完成工作 . 不想导出,跳过此参数可在导航资源管理器中打开报告服务 .