Monday, 22 June 2015

Microsoft Report in MVC 4

INTRODUCTION

In this post I am explain how to use Microsoft Report in MVC 4. Most of the developer use Microsoft Report (rdlc) for generating report  in asp.net application. Here I have explained how we can use Microsoft Report (rdlc) in MVC.Just follow 10 easy steps and get result.

PREREQUISITE

I used followings:
  • .Net framework 4.0
  • Entity Framework
  • Sql Server 2008

STEPS :

Just follow the steps and get result easily. 

STEP - 1 : CREATE NEW PROJECT

Go to File > New > Project > Select asp.net mvc4 web application > Entry Application Name > Click OK.

STEP-2: ADD A DATABASE.

Go to Solution Explorer > Right Click on App_Data folder > Add > New item > Select SQL Server Database Under Data > Enter Database name > Add.

STEP-3: CREATE TABLE AND INSERT DATA FOR SHOW IN REPORT

Open Database > Right Click on Table > Add New Table > Add Columns > Save > Enter table name > Ok. 

STEP-4: ADD ENTITY DATA MODEL.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select ADO.net Entity Data Model under data > Enter model name > Add.
A popup window will come (Entity Data Model Wizard) > Select Generate from database > Next >
Chose your data connection > select your database > next > Select tables > enter Model Namespace > Finish. 

STEP-5: ADD ACTION FOR POPULATE DATA.

Go To Controller > Add your action > write following code and Rebuild your application to get data from Database. 
                   

public ActionResult StateArea()
        {
            using (PopulationEntities dc =      new PopulationEntities())
            {
                var v = dc.StateAreas.ToList();
                return View(v);
            }
        }

STEP-6: ADD VIEW FOR SHOW DATA ON PAGE.

Right Click on your Action > Add View > Enter View name > Check Create a strongly-type view > Select your model class > Select Scaffold templete > Select list > Add. 

View


@model IEnumerable<MvcReportViwerApp.StateArea>
@{
    ViewBag.Title = "State Area";
}
<h2>State Area - Report</h2>

<table>
    <tr>
        <th> State ID</th>
        <th>State Name</th>
        <th>Area(KM)</th>
    </tr>
@foreach (var item in Model) {
    <tr>
        <td>@Html.DisplayFor(modelItem => item.StateID)</td>
        <td>@Html.DisplayFor(modelItem => item.Statename)</td>
        <td>@Html.DisplayFor(modelItem => item.Area)</td>
    </tr>
}
</table>
<div style="padding:10pxborder:1px solid black">
    <div><a href="@Url.Action("Report",new {id= "PDF"})"> Get Report PDF</a></div>
    <div><a href="@Url.Action("Report",new {id= "Excel"})"> Get Report Excel</a></div>
    <div><a href="@Url.Action("Report",new {id= "Word"})"> Get Report Word</a></div>
    <div><a href="@Url.Action("Report",new {id= "Image"})"> Get Report Image</a></div>
</div>


  
Run Application.

Look Result show in your browser.
Now Add some links to your view for generate PDF,Excel,Word files containing report data. Add this to your view.

STEP-7: ADD REFERENCE (MICROSOFT.REPORTVIWER.WEBFORMS.DLL)

Right Click on references under project folder > Add Reference > Select Microsoft.ReportViwer.WebForms.dll > OK. 

STEP-8: ADD REPORT FILE(.RDLC) AND DESIGN YOUR REPORT.

Add report folder to your project 
Right Click on report folder > Add > New item > Select Report under Reporing > Enter report file name > Add.
Here we also have to add Datasource for our report.
Under report data Click on New > Dataset > New > Choose Data Connection > Next > Select Table > Finish.
Now Design your Report looks.

STEP-9: ADD ACTION FOR GENERATE PDF, EXCEL, WORD AND IMAGE FILE FOR REPORT DATA

Right Click on your Action > Add View > Enter View name > Check Create a strongly-type view > Select your model class > Select Scaffold templete > Select list > Add. Go To Controller > Add your action > write following code and Rebuild your application to get data from Database. 
                        
    

public ActionResult Report(string id)
        {
            LocalReport lr = new LocalReport();
            string path = Path.Combine(Server.MapPath("~/Report"),"ReportStateArea.rdlc");
            if (System.IO.File.Exists(path))
            {
                lr.ReportPath = path;
            }
            else
            {
                return View("Index");
            }
            List<StateArea> cm = new List<StateArea>();
            using (PopulationEntities dc = new PopulationEntities())
            {
                cm = dc.StateAreas.ToList();
            }
            ReportDataSource rd = new ReportDataSource("MyDataset", cm);
            lr.DataSources.Add(rd);
            string reportType = id;
            string mimeType;
            string encoding;
            string fileNameExtension;



            string deviceInfo =

            "<DeviceInfo>" +
            "  <OutputFormat>" + id + "</OutputFormat>" +
            "  <PageWidth>8.5in</PageWidth>" +
            "  <PageHeight>11in</PageHeight>" +
            "  <MarginTop>0.5in</MarginTop>" +
            "  <MarginLeft>1in</MarginLeft>" +
            "  <MarginRight>1in</MarginRight>" +
            "  <MarginBottom>0.5in</MarginBottom>" +
            "</DeviceInfo>";

            Warning[] warnings;
            string[] streams;
            byte[] renderedBytes;

            renderedBytes = lr.Render(
                reportType,
                deviceInfo,
                out mimeType,
                out encoding,
                out fileNameExtension,
                out streams,
                out warnings);


            return File(renderedBytes, mimeType);
        }
STEP-10: RUN APPLICATION
Click on links and get your report in PDF, Excel, Word and Image format.

0 comments: