ASP.Net - MVC C# | Report : Microsoft Reporting - Implement RDLC in MVC

 

ASP.Net - MVC C#| Report : Microsoft Reporting

- Implement RDLC in MVC



Here, I have demonstrate, how to implement Microsoft reporting in MVC web application. Please follow below steps to achieve reporting.

Step1 : Create web application with blank and select code reference "MVC". I have created project name with "DemoReport".

Step 2: Add empty controller name what you want. I have create "Home" and "Country".

Step 3: Add Entity Data Model follow screenshots.













Step 4: Add below code in Index action of Country controller
public ActionResult Index()
        {
            List<Country> countryList = null;
 
            using (DemoReportEntities db = new DemoReportEntities())
            {
 
                countryList = db.Countries.OrderBy(o=>o.CountryName).Take(10).ToList();
            }
            return View(countryList);
        }

fda


Step 5: Add below html code in Index view of Country
@model IEnumerable<DemoReport.Models.Country>
@{
    ViewBag.Title = "Country";
}
 
<div class="box">
    <div class="box-header with-border">
        <h3 class="box-title">Country List</h3>
    </div>
    <div class="row">
        <div class="col-md-6">
            <div class="box-body">
                <table class="table table-bordered">
                    <tbody>
                        <tr>
                            <th>Country Name</th>
                            <th>Country Code</th>
                        </tr>
                        @foreach (var item in Model)
                        {
                            <tr>
                                <td>@Html.DisplayFor(m => item.CountryName)</td>
                                <td>
                                    @Html.DisplayFor(m => item.CountryCode)
                                </td>
                            </tr>
                        }
                    </tbody>
                </table>
            </div>
        </div>
        <div class="col-md-6">
            <div><a href="@Url.Action("GenerateReport",new {id= "PDF"})" class="btn btn-primary btn-Report"> Get Report PDF</a></div>
            <div><a href="@Url.Action("GenerateReport",new {id= "Excel"})" class="btn btn-primary btn-Report"> Get Report Excel</a></div>
            <div><a href="@Url.Action("GenerateReport",new {id= "Word"})" class="btn btn-primary btn-Report"> Get Report Word</a></div>
            <div><a href="@Url.Action("GenerateReport",new {id= "Image"})" class="btn btn-primary btn-Report"> Get Report Image</a></div>
        </div>
    </div>
</div>

 Step 6: Add css class in site.css or your stylesheet.
.btn.btn-primary.btn-Report {width:150px !importantmargin:3px


Step 7: Add below html code in _Layout.cshtml for menu option
<div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li>
                        @Html.ActionLink("Country List""Index""Country"new { area = "" }, new { @class = "navbar-brand" })
                    </li>
                </ul>
            </div>


Step 8: Run application and it will give output as below,

 

Step 9: Add new folder in project for report, I added "Reports", then follow  below steps



 
Step 10: Add new folder in project for report data sets, I added "ReportDataSet", then follow  below steps














Step 11: Add data set to Report,




 
Step 12: Add header and footer in report


 
Step 13: Add report assembly reference in project.

Step 14: Lastly write code to generate report file in pdf, word, excel or image. create new action method in country controller and write below code.

public ActionResult GenerateReport(string id)
        {
            List<Country> data = null;
            using (DemoReportEntities db = new DemoReportEntities()){
                data = db.Countries.OrderBy(o => o.CountryName).Take(10).ToList();
            }
 
            LocalReport lr = new LocalReport();
            string path = Path.Combine(Server.MapPath("~/Reports"), "CountryReport.rdlc");
            if (System.IO.File.Exists(path)){
                lr.ReportPath = path;
            }
            else{
                return View("Index", data);
            }
 
            ReportDataSource rd = new ReportDataSource("ReportCountryDataSet", data);
            lr.DataSources.Add(rd);
            string reportType = id;
            string mimeType, encoding, 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 15: Finally run the project and click on Generate Report button. See generated pdf.


I hope that you like the reporting tutorial.

Enjoy !!!

:)

 

 


No comments:

Post a Comment