Crystal Reports in ASP.NET MVC

2019-01-04 17:28发布

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?

3条回答
别忘想泡老子
2楼-- · 2019-01-04 18:05

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.

查看更多
beautiful°
3楼-- · 2019-01-04 18:13

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
    
查看更多
smile是对你的礼貌
4楼-- · 2019-01-04 18:22

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");


            }
查看更多
登录 后发表回答