我知道在 ASP.NET MVC 中不允许使用服务器端控件,但是我们有一长串水晶报告,该公司已经为以前的应用程序制作了这些报告,我想将这些报告用于我们的新 ASP。 NET MVC 应用程序。
有没有合适的方法在 ASP.NET MVC 中使用水晶报表?如果是这样,怎么做?
我知道在 ASP.NET MVC 中不允许使用服务器端控件,但是我们有一长串水晶报告,该公司已经为以前的应用程序制作了这些报告,我想将这些报告用于我们的新 ASP。 NET MVC 应用程序。
有没有合适的方法在 ASP.NET MVC 中使用水晶报表?如果是这样,怎么做?
其实很简单。只需将以下引用添加到您的 MVC 项目:
使用如下的 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
我们在工作中有/有过类似的情况。
我们使用的解决方案:
我们没有看到这个设置有任何问题(除了普通的水晶问题)。
只需添加此参考:使用 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");
}