asp.net-mvc ASP.NET MVC 中的水晶报表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/348785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:12:11  来源:igfitidea点击:

Crystal Reports in ASP.NET MVC

asp.net-mvccrystal-reports

提问by Odd

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.

我知道在 ASP.NET MVC 中使用服务器端控件是禁忌,但是我们有一长串水晶报告,公司已经为我想用于我们新 ASP 的以前的应用程序制作了这些报告。 NET MVC 应用程序。

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

有没有合适的方法在 ASP.NET MVC 中使用水晶报表?如果是这样,如何?

采纳答案by leppie

We had/have a similar situation at work.

我们在工作中曾经/有过类似的情况。

The solution we use:

我们使用的解决方案:

  • Create a seperate directory for reports
  • Create normal ASPX pages for reports
  • 为报告创建一个单独的目录
  • 为报告创建普通的 ASPX 页面

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

我们没有看到任何问题(除了普通的 Crystal 问题)。

回答by coderguy123

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

其实很简单。只需将以下引用添加到您的 MVC 项目中:

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

use Action method like below:

使用 Action 方法,如下所示:

  • 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
    
  • 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
    

回答by Sofia

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

只需添加此参考:使用 CrystalDecisions.CrystalReports.Engine;
比做这个动作:

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


            }