How do I load external Crystal Reports (2008) files in c#?
Can someone tell me if it's possible开发者_如何学Python to load external CrystalReports (2008) .rpt files? I now embed them so they're compiled with my core.
It would be nice if i can make changes to a reports layout without having to recompile anything. Is that possible?
Thanks.
Yes, this is possible. In the following example, the .rpt file can be uploaded and then set as the source of a standard ASP.NET CrystalReportViewer
control (in this case, reportViewer
).
using CrystalDecisions.CrystalReports.Engine;
...
ReportDocument document = new ReportDocument();
document.Load(reportPath); //"C:\\path\\to\\report\\file.rpt";
reportViewer.ReportSource = document;
reportViewer.RefreshReport();
If the database credentials embedded in the .rpt file are incorrect, they can be set as follows:
using CrystalDecisions.Shared;
...
private void ViewReport()
{
ConnectionInfo connInfo = new ConnectionInfo();
connInfo.ServerName = "dbservername";
connInfo.DatabaseName = "dbname";
connInfo.UserID = "dbusername";
connInfo.Password = "dbpassword";
reportViewer.ReportSource = GetReportSource(connInfo);
reportViewer.RefreshReport();
}
private ReportDocument GetReportSource(ConnectionInfo connInfo)
{
ReportDocument document = new ReportDocument();
document.Load(reportPath); //"C:\\path\\to\\report\\file.rpt";
TableLogOnInfos logonInfos = new TableLogOnInfos();
TableLogOnInfo logonInfo = new TableLogOnInfo();
Tables tables;
tables = document.Database.Tables;
foreach(CrystalDecisions.CrystalReports.Engine.Table table in tables)
{
logonInfo = table.LogOnInfo;
logonInfo.ConnectionInfo = connInfo;
table.ApplyLogOnInfo(logonInfo);
}
return document;
}
精彩评论