65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 C# 从 SharePoint 站点访问 SSRS 报告

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.93/5 (5投票s)

2013年12月20日

CPOL

1分钟阅读

viewsIcon

37171

使用 C# 从 SharePoint 站点访问 SSRS 报告

引言

本技巧演示了如何使用 C# 访问存储在 SharePoint 站点上的 SSRS 报告。它主要侧重于使用 Windows Forms / WPF 开发的桌面应用程序来演示这一点。

在本文中,我们将学习如何

  • 使用 .NET 中的 ReportViewer 控件显示 SSRS 报告。
  • 将 SSRS 报告转换为 PDF 附件,该附件可由 .NET 中的 SMTPClient 对象用于将此报告作为电子邮件附件(PDF)发送。
  • 将报告保存为 PDF。

背景

Microsoft 报告可以使用两种不同的方法设计——作为本地报告和作为服务器报告。对于设计本地报告,报告数据集(.XSD 文件)必须是使用该报告的 .NET 项目的一部分。如果报告的设计或呈现的数据发生任何更改,则需要重新编译和重新部署整个应用程序。

但是,如果报告开发为服务器报告,则可以消除此问题。我们将把 SSRS 报告(本地构建)存储在 SharePoint 站点上,并使用 C# 通过 WPF 构建的桌面应用程序访问此服务器报告。

Using the Code

在我们的示例中,我们有一个名为 SSRSReport 的类,该类具有不同的方法来执行本文介绍部分中提到的任务。

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

关注点

目前尚未发现任何问题。 看起来是一个相当简单的过程。

历史

这是 DLL 的第一个版本。 随着我们发现错误和/或附加功能,将会进行更多更新。

© . All rights reserved.