35

I know the use of server-side controls is a no-no in ASP.NET MVC, however we have a long list of crystal reports that the company has already produced for a previous application that I would like to utilize for our new ASP.NET MVC application.

Is there an appropriate way to use crystal reports in ASP.NET MVC? If so, how?

Odd
  • 4,737
  • 6
  • 30
  • 27

3 Answers3

76

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
    
Matthieu
  • 4,605
  • 4
  • 40
  • 60
coderguy123
  • 1,955
  • 1
  • 15
  • 15
15

We had/have a similar situation at work.

The solution we use:

  • Create a seperate directory for reports
  • Create normal ASPX pages for reports

We have not seen any issues (besides the normal Crystal ones) with this setup.

leppie
  • 115,091
  • 17
  • 196
  • 297
  • 3
    How do you exclude the directory from the routes? – Odd Dec 08 '08 at 06:33
  • Actually, I just checked. No need to modify the route. – leppie Dec 08 '08 at 07:10
  • If you added a Crystal report in asp.net MVC then have you encountered problem of Refreshing Crystal report? Have you faced "Validation of viewstate MAC failed error" ? – Vikas May 22 '09 at 10:26
  • 1
    You'll have to deal with authorization somehow with this approach. – c.sokun Jul 18 '11 at 16:09
  • @Vikas are you using that "CrystalReportSource" thing? If so use ReportDocument and save you report document and maybe it will be better – Nick Jul 26 '13 at 21:13
  • @leppie can you please add more details and help me how to pass multiple parameters from MVC view to webform and print crystal reports , please check my question and help me https://stackoverflow.com/questions/62498523/how-to-transfer-this-code-from-asp-net-to-asp-net-mvc-to-print-crystal-reports?noredirect=1#comment110528249_62498523 – Abdullah Jun 21 '20 at 18:15
2

Just add this reference : using CrystalDecisions.CrystalReports.Engine;
than do this action :

using CrystalDecisions.CrystalReports.Engine;  
    public ActionResult Report()
            {
                List<Table> table = new List<Table>();
                ReportDocument rd = new ReportDocument();
                rd.Load(Path.Combine(Server.MapPath("~/Repport/CrystalReport1.rpt")));

                Response.Buffer = false;
                Response.ClearContent();
                Response.ClearHeaders();
                Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
                stream.Seek(0, SeekOrigin.Begin);


                return File(stream, "application/pdf", "Suivie Historique.pdf");


            }
Sofia
  • 159
  • 2
  • 9
  • 2
    **From review queue**: May I request you to please add some context around your source-code. Code-only answers are difficult to understand. It will help the asker and future readers both if you can add more information in your post. – RBT May 24 '17 at 01:52