RDLC reports in mvc3 application

Reports should be generated in my MVC3 application. How can I use RDLC in mvc3. Please, someone can give me an explanation and recommendations for the subsequent creation of RDLC reports in my MVC3 application.

thanks

+4
source share
3 answers

I recently used RDLC reports in an MVC3 application to export results to 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 a report exported as an excel document.

+4
source

This is not an easy task! Here are six steps for you.

1. Web.config

To get started, open your web.config and add the following to the <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 in the <compilation> 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 in the <system.webServer> add the following:

 <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 add a link to Microsoft.ReportViewer.Common.dll and Microsoft.ReportViewer.WebForms.dll (version 10.0.0.0).

2. Global.asax

Open the Global.asax.cs function 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 are doing this, create a new folder named "ReportViewer" in your asp.net application.

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 must be empty.

Right-click on it and select "Add", "Table" ...

Select the database connection string. Save it in the application configuration. Use an existing stored procedure. Fill the data set using the SELECT query. Give the dataset a meaningful name.

4. Create an RDLC report

Create a new RDLC report file (you will need BI tools for this) and go to "Add Dataset". Select a new dataset. Create a report. Pay attention to the name of the dataset. (For example, "DataSet1", be sure to change it later to something more useful.)

5. Customize the ASPX page

Create a new ASP.NET web form, yes, a web page called ShowReport.aspx. 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

Inside codebehind for your reports.aspx.cs do the following:

  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 the page:

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

Voila!

+4
source

You will have to manually generate the report, as in the example above. You can use pdf so that the user can view it in a browser (if acrobat is installed) instead of downloading it first.

0
source

All Articles