Crystal Reports in ASP.NET MVC

It is pretty simple actually. just add following references to your MVC project:

  • CrystalDecisions.CrystalReports.Engine
  • CrystalDecisions.ReportSource
  • CrystalDecisions.Shared

use Action method like below:

  • C# :

    using CrystalDecisions.CrystalReports.Engine;
    
    public ActionResult Report()
        {
            ReportClass rptH = new ReportClass();
            rptH.FileName = Server.MapPath("[reportName].rpt");
            rptH.Load();
            rptH.SetDataSource([datatable]);
            Stream stream = rptH.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
            return File(stream, "application/pdf");   
        }
    
  • VB.NET :

     Imports CrystalDecisions.CrystalReports.Engine
    
     Public Function Report() As ActionResult
        Dim rptH As New ReportClass()
        rptH.FileName = Server.MapPath("[reportName].rpt")
        rptH.Load()
        rptH.SetDataSource([datatable])
        Dim stream As IO.Stream = rptH.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat)
        Return File(stream, "application/pdf")
     End Function
    

Leave a Comment

tech