How To Use Crystal Report In MVC.NET

In this article, we will learn how to use Crystal Reports in MVC.NET. We will create a PDF file and will export it using Crystal Reports.

Introduction
 
In this post, we will learn how to create a PDF file from the data and export it using Crystal Reports in MVC.NET.
Prerequisites
  • Visual Studio
  • SQL Server
  • Crystal Report (Download from here)
Step 1 - Create Crystal Report

In an MVC.NET project, on the root, create a folder for reports. Then, right click on that folder and select a new item. After that, create a Crystal Report file by following the below steps.
 
How To Use Crystal Report In MVC.NET 
 
After clicking on the "Add" button, a new popup will open. In there, apply the below steps.
 
How To Use Crystal Report In MVC.NET 
 
The Crystal Report will open like in the below image.
 
How To Use Crystal Report In MVC.NET 
 
Then, you need to set the table into Crystal Report for displaying records. For that, follow the below steps.
 
Step 2 - Set Table 
 
Right-click "Database Fields" from the Field Explorer and select the "Database Expert" option.
 
 How To Use Crystal Report In MVC.NET
 
A new popup will open; apply the following steps.
How To Use Crystal Report In MVC.NET
 
On clicking OK, you will see the selected tables into Database Fields.
 
How To Use Crystal Report In MVC.NET
 
After that, you need to drag fields to the report which you want to display in the PDF file, as shown below.

How To Use Crystal Report In MVC.NET 
Step 3 - Create a method in Controller
 
Create a method for returning the PDF file from Crystal Report.
  1. using CrystalDecisions.CrystalReports.Engine;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Data;  
  5. using System.IO;  
  6. using System.Linq;  
  7. using System.Text.RegularExpressions;  
  8. using System.Web;  
  9. using System.Web.Mvc;  
  10. using System.Xml.Linq;  
  11. using temp.Models;  
  12.   
  13. namespace temp.Controllers  
  14. {  
  15.     public class HomeController : Controller  
  16.     {  
  17.         public ActionResult Index()  
  18.         {  
  19.             return View();  
  20.         }  
  21.   
  22.         public ActionResult Download_PDF()  
  23.         {  
  24.             empEntities context = new empEntities();  
  25.   
  26.             ReportDocument rd = new ReportDocument();  
  27.             rd.Load(Path.Combine(Server.MapPath("~/Report"), "Emp_Data.rpt"));  
  28.             rd.SetDataSource(context.emp_table.Select(c => new  
  29.             {  
  30.                 id = c.id,  
  31.                 name = c.name  
  32.             }).ToList());  
  33.   
  34.             Response.Buffer = false;  
  35.             Response.ClearContent();  
  36.             Response.ClearHeaders();  
  37.   
  38.   
  39.             rd.PrintOptions.PaperOrientation = CrystalDecisions.Shared.PaperOrientation.Landscape;  
  40.             rd.PrintOptions.ApplyPageMargins(new CrystalDecisions.Shared.PageMargins(5, 5, 5, 5));  
  41.             rd.PrintOptions.PaperSize = CrystalDecisions.Shared.PaperSize.PaperA5;  
  42.   
  43.             Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);  
  44.             stream.Seek(0, SeekOrigin.Begin);  
  45.   
  46.             return File(stream, "application/pdf""CustomerList.pdf");  
  47.         }  
  48.   
  49.     }  
  50. }  
Here, I am creating a Download_PDF() method for returning the PDF file using Crystal Report.
  1. ReportDocument rd = new ReportDocument();  
It will create a Crystal Report's object "rd" and using this object, load the Crystal Report like below.
  1. rd.Load(Path.Combine(Server.MapPath("~/Report"), "Emp_Data.rpt"));  
Here, "context" is my entity's object and I will get the emp_table's data using this object by just using the below code.
  1. context.emp_table.Select(c => new  
  2.             {  
  3.                 id = c.id,  
  4.                 name = c.name  
  5.             }).ToList()  
 and finaly, this line returns the PDF file.
  1. return File(stream, "application/pdf""CustomerList.pdf");  
Step 5 - Call the method of Download PDF from the view side. 
  1. <a href="Home/Download_PDF/" target="_blank" class="btn btn-primary" style="margin-top:20px;">Download_PDF</a>      
Here, I have set the button "Download PDF" and on clicking it, the PDF file gets downloaded.
 
Output
 
How To Use Crystal Report In MVC.NET