Tuesday, August 17, 2010

Crystal Reports XI - Export

I have been working with Excel & Word export with ASP.NET using MIME types for a long time...

I got a oppertunity to work with PDF & Excel Export using Crystal Reports XI. Cool, it has lot of flexibility and feature that can help us to export the reports in diffeent formats


EnterpriseService ceEnterpriseService;
InfoStore ceInfoStore;
InfoObjects ceReportObjects;
InfoObject ceReportObject;
Report ceReport;

protected CrystalDecisions.CrystalReports.Engine.ReportDocument reportDocument1;
reportDocument1 = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
reportDocument1.FileName = "";

SessionMgr ceSessionMgr = new SessionMgr();
EnterpriseSession ceSession;

ceSession = ceSessionMgr.Logon(CommonFunctions.GetConFigValue("CrystalUserId"), CommonFunctions.GetConFigValue("CrystalPwd"), CommonFunctions.GetConFigValue("CrystalServerName"), "secEnterprise");
ceEnterpriseService = ceSession.GetService("", "InfoStore");
ceInfoStore = new InfoStore(ceEnterpriseService);
sQuery = "Select * From CI_INFOOBJECTS Where SI_PROGID = 'CrystalEnterprise.Report' AND SI_NAME='" + strReportName + "'";
ceReportObjects = ceInfoStore.Query(sQuery);

if (ceReportObjects.Count > 0)
{
ceReportObject = ceReportObjects[1];
ceReport = ((Report)(ceReportObject));
reportDocument1.Load(ceReportObject, ceSession);


MemoryStream oStream; // using System.IO
oStream = (MemoryStream)reportDocument1.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/pdf";
Response.BinaryWrite(oStream.ToArray());
Response.End();

}

else
{
Response.Write("No report objects found by query
");
}


}
else
{
Response.Write("No Valid Enterprise Session Found!
");
}

No comments:

Post a Comment