Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RDLC Reports in mvc3 application

Reports are required to be generated in my MVC3 application, How can i use RDLC in mvc3. Please anyone can give me an examplary explaination and guidelines to follow to create the RDLC reports in my MVC3 application.

thanks

like image 722
ziaullah Avatar asked Jan 18 '23 22:01

ziaullah


2 Answers

I have recently used RDLC reports in an MVC3 App to export results in an Excel spreadsheet.

    private class ExcelReport 
    {
        private string encoding;
        private string[] streams;
        private Warning[] warnings; 
        private string fileNameExtension;
        private string mimeType; 
        public ExcelReport()
        {
            this.ReportDataSources = new List<ReportDataSource>();
        }
        public string ExportFileName { get; set; }
        public string MimeType
        {
            get
            {
                return mimeType;
            }
            private set
            {
                mimeType = value;
            }
        }
        public string Encoding
        {
            get
            {
                return encoding;
            }
            private set
            {
                encoding = value;
            }
        }
        public string FileNameExtension
        {
            get
            {
                return fileNameExtension;
            }
            private set
            {
                fileNameExtension = value;
            }
        }
        public string[] Streams
        {
            get
            {
                return streams;
            }
            private set
            {
                streams = value;
            }
        }
        public Warning[] Warnings
        {
            get
            {
                return warnings;
            }
            private set
            {
                warnings = value;
            }
        }
        public string ReportPath { get; set; }

        public IList<ReportDataSource> ReportDataSources { get; set; }
        public byte[] GetReport() 
        {

            LocalReport localReport = new LocalReport();
            localReport.ReportPath = this.ReportPath;

            foreach (var source in this.ReportDataSources)
            {
                localReport.DataSources.Add(source);
            }

            string reportType = "Excel";

            //The DeviceInfo settings should be changed based on the reportType             
            //http://msdn2.microsoft.com/en-us/library/ms155397.aspx             
            string deviceInfo =
                "<DeviceInfo>" +
                "  <OutputFormat>Excel</OutputFormat>" +
                "  <PageWidth>21cm</PageWidth>" +
                "  <PageHeight>29cm</PageHeight>" +
                "  <MarginTop>1cm</MarginTop>" +
                "  <MarginLeft>2cm</MarginLeft>" +
                "  <MarginRight>2cm</MarginRight>" +
                "  <MarginBottom>1cm</MarginBottom>" +
                "</DeviceInfo>";


            //Render the report             
            return localReport.Render(
                reportType,
                deviceInfo,
                out mimeType,
                out encoding,
                out fileNameExtension,
                out streams,
                out warnings
            );
        }
    }

Controller

    public ActionResult GetReport(string reportParameter1) 
    {
        /*Get data for your report that matches the dataset format in the report.*/
        IEnumerable<ReportData> list = new IEnumerable<ReportData> /*Your Report Data*/
                                          {
                                             new ReportData{Id = 1, Code="ABC"},
                                             new ReportData{Id = 2, Code="DEF"}

                                          };
        var excelReport = new ExcelReport
        {
            ExportFileName = "Your File Name",
            ReportPath = Server.MapPath("~/Content/Reports/YourReport.rdlc")

        };
        var ds = new ReportDataSource("Main", list); /* Main is the name of the dataset inside the report*/
        excelReport.ReportDataSources.Add(ds);

        var report = excelReport.GetReport();

        Response.AddHeader("content-disposition", string.Format("attachment; filename={0}.{1}", excelReport.ExportFileName, excelReport.FileNameExtension));
        Response.ContentType = "application/vnd.ms-excel";

        return File(report, excelReport.MimeType);
    }

The end result should be you report exported as an excel docuemnt.

like image 61
Nathan Fisher Avatar answered Feb 01 '23 14:02

Nathan Fisher


This is not a simple task! Here's six steps for you to go through.

1. Web.config

To start with, open your web.config and add the following into your <system.web> section

<httpHandlers>
<add path="Reserved.ReportViewerWebControl.axd" verb="*" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
    validate="false" />
</httpHandlers>

and then within your <compilation> tag, add the following:

  <buildProviders>
    <add extension=".rdlc" type="Microsoft.Reporting.RdlBuildProvider, Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
  </buildProviders> 

and then inside your <system.webServer> tag add this:

<handlers>
  <add name="ReportViewerWebControlHandler" preCondition="integratedMode" verb="*" path="Reserved.ReportViewerWebControl.axd" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</handlers>

and also add a reference to Microsoft.ReportViewer.Common.dll and Microsoft.ReportViewer.WebForms.dll (version 10.0.0.0) as well.

2. Global.asax

Open your Global.asax.cs and in the RegisterRoutes() function add the following:

//Reports Viewers depend on Viewstate so are hosted in class ASPX web forms, so bypassing MVC3 routing
routes.IgnoreRoute("ReportViewer/");  

While you're doing this, create a new folder named "ReportViewer" in your asp.net app.

3. XSD dataset

Create a new XSD file in the ROOT of your project folder (yes, it must be the root!).

Right click on your project, click "Add new DataSet". It should be empty.

Right click on it and go to Add, TableAdapter...

Choose your database connection string. Save it in the app config. Use an existing stored proc. Populate the dataset using a SELECT query. Give the dataset a meaningful name.

4. Create your RDLC Report

Create a new RDLC report file (you'll need the BI tools for this), and go "Add dataset". Choose your new dataset. Create the report. Make note of the dataset name. (Eg, "DataSet1", remember to change it later to something more useful.)

5. Setup the ASPX page

Create a new ASP.NET WebForm, yes, webform page titled ShowReport.aspx. The HTML should have the following:

<body>
<form id="form1" runat="server" >
    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
    <rsweb:ReportViewer  
        ID="ReportViewer1"  
        SizeToReportContent="False" 
        Width="820px"
        Height="820px"
        runat="server"  
        ShowRefreshButton="false"
        AsyncRendering="false" 
        DocumentMapCollapsed="True" 
        PageCountMode="Actual" 
        PromptAreaCollapsed="True"></rsweb:ReportViewer>
</form>
</body>

6. Codebehind

Within the codebehind for your reports.aspx.cs, do this:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
                DataSet dataset  =  GetPopulatedDatasetFromDB()    // Populate your dataset here by calling the stored proc.

                ReportViewer1.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Local;
                ReportViewer1.LocalReport.ReportPath = Server.MapPath("/Reports/MyNewReport.rdlc");
                ReportViewer1.LocalReport.DataSources.Clear();
                ReportViewer1.LocalReport.DataSources.Add(
                    new Microsoft.Reporting.WebForms.ReportDataSource("DataSet1", dataset.Tables[0]));

                ReportViewer1.LocalReport.Refresh();
        }
    }

Visit your page:

http://localhost/MyApp/ReportViewer/ShowReport.aspx

Voila!

like image 39
Rocklan Avatar answered Feb 01 '23 15:02

Rocklan