Monday, 9 May 2016

Call SSRS Report from MVC

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                ShowReport();
        }

        private void ShowReport()
        {
            try
            {
                string printFormat = string.Empty;
                string userId = string.Empty;
                string invoiceDate = string.Empty;

                byte[] returnValue = null;
                string deviceinfo = "";
                string mimeType = "";
                string encoding = "";
                string[] streams = null;
                Microsoft.Reporting.WebForms.Warning[] warnings = null;

                if (Request.QueryString["format"] != null)
                {
                    printFormat = Request.QueryString["format"];
                }

                if (Request.QueryString["userId"] != null)
                {
                    userId = Request.QueryString["userId"];
                }

                if (Request.QueryString["invoiceDate"] != null)
                {
                    invoiceDate = Request.QueryString["invoiceDate"];
                }

                //report url 
                string urlReportServer = WebConfigurationManager.AppSettings["UrlReportServer"];

                // ProcessingMode will be Either Remote or Local 
                rptViewer.ProcessingMode = ProcessingMode.Remote;

                //Set the ReportServer Url 
                rptViewer.ServerReport.ReportServerUrl = new Uri(urlReportServer);

                // setting report path 
                //Passing the Report Path with report name no need to add report extension  
                rptViewer.ServerReport.ReportPath = "/TestReports/ReportName";

                List<ReportParameter> reportParams = new List<ReportParameter>();
                reportParams.Add(new ReportParameter("UserId", userId));
                reportParams.Add(new ReportParameter("InvoiceDate", invoiceDate));

                rptViewer.ServerReport.SetParameters(reportParams);

                rptViewer.ServerReport.Refresh();

                if (printFormat == "Xls")
                {
                    //output as XLS
                    string format = "Excel";
                    string extension = "xls";
                    returnValue = rptViewer.ServerReport.Render(format, deviceinfo, out mimeType, out encoding, out extension, out streams, out warnings);
                    Response.Buffer = true;
                    Response.Clear();
                    Response.ContentType = "application/ms-excel";
                    string fileName = "TestFileName" + DateTime.Now.ToString("yyyyMMddHHmmssfff");
                    Response.AddHeader("content-disposition", "attachment; filename=" + fileName + ".xls");
                    Response.BinaryWrite(returnValue);
                    Response.Flush();
                    Response.End();
                }
                else
                {
                    //output as PDF
                    string format = "PDF";
                    string extension = "pdf";

                    returnValue = rptViewer.ServerReport.Render(format, deviceinfo, out mimeType, out encoding, out extension, out streams, out warnings);
                    Response.Buffer = true;
                    Response.Clear();

                    Response.ContentType = mimeType;
                    string fileName = "TestFileName" + DateTime.Now.ToString("yyyyMMddHHmmssfff");
                    Response.AddHeader("content-disposition", "attachment; filename=" + fileName + ".pdf");

                    Response.BinaryWrite(returnValue);
                    Response.Flush();
                    Response.End();
                }

            }
            catch (Exception)
            {
                throw;
            }
        }       
   

No comments:

Post a Comment