moopoo.net
Fishing, technology and anything else

Exporting Crystal Reports from the web.

October 23rd, 2007 by Matt

Exporting Crystal Reports from a report displayed in the browser can be a pain, not least because it’s long winded, but also that I primarily use Firefox and the report rendering in anything other than IE is a joke.

For a while I have been trying to work out how I could export Crystal Reports in various formats such as Excel and PDF without having to render the report in the web browser first. Some of our customers wanted the data to put into their spreadsheet application so they could manipulate the data to their hearts content and I wanted to still use the power of Crystal Reports as it meant that the people who are good at writing the reports could do it and save me the work.

I’d seen it done in Crystal 8 but had not seen anything that pulled it all together in XI. I think I was looking a little too hard, the solution was in fact pretty straight forward.

The method employed is pretty similar to the post I made on displaying Crystal Reports in the browser, with only a couple of exceptions.

Firstly, you don’t need to employ the ReportViewer control as the report is not being rendered to the browser.

// Declare variables
private ReportDocument document1 = new ReportDocument();
private TableLogOnInfos infos1 = new TableLogOnInfos();
private TableLogOnInfo info2 = new TableLogOnInfo();
private ConnectionInfo info1 = new ConnectionInfo();
 
//  default export type
ExportFormatType expFormat = new ExportFormatType();
expFormat = ExportFormatType.PortableDocFormat;
 
// load the report
document1.Load(Request.PhysicalApplicationPath + “myReport.rpt);
 
// Add the parameters
document1.SetParameterValue(“param1″, “value1″);
document1.SetParameterValue(“param2″, “value2″);
 
//Set the login info for the database
info1.ServerName = ConfigurationSettings.AppSettings[“server_name”];
info1.DatabaseName = ConfigurationSettings.AppSettings[“database_name”];
info1.UserID = ConfigurationSettings.AppSettings[“user_id”];
info1.Password = ConfigurationSettings.AppSettings[“password”];
Tables tables1 = document1.Database.Tables;
 
//Apply login info to the report
foreach (CrystalDecisions.CrystalReports.Engine.Table table1 in tables1)
{
    info2 = table1.LogOnInfo;
    info2.ConnectionInfo = info1;
    table1.ApplyLogOnInfo(info2);
}
 
// Export the document
HttpResponse Response = HttpContext.Current.Response;
document1.ExportToHttpResponse(expFormat,Response,true,”Exported Report”);
document1.Close();

Posted in Web Development

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.