CodeKicks.com
Focus on Microsoft Technologies - Tutorials, Articles, Code Samples.

Tuesday, March 13, 2007

Exporting in Crystal Reports with .NET : Solving problems with "HTML" file export

private void exportReport(ReportDocument selectedReport, string selectedExportOption)


{


try


{


string contentType ="";


ExportFormatType eft = new ExportFormatType();


// Make sure asp.net has create and delete permissions in the directory


string tempFileName = System.Configuration.ConfigurationSettings.AppSettings["TempDir"] + Session.SessionID.ToString() + ".";


switch (selectedExportOption)


{


case "PortableDocFormat" :


eft = ExportFormatType.PortableDocFormat;


tempFileName += "pdf";


contentType = "application/pdf";


break;


case "WordForWindows" :


eft = ExportFormatType.WordForWindows;


tempFileName+= "doc";


contentType = "application/msword";


break;


case "Excel" :


eft = ExportFormatType.Excel;


tempFileName+= "xls";


contentType = "application/vnd.ms-excel";


break;


case "RichText" :


eft = ExportFormatType.RichText;


tempFileName+= "rtf";


contentType = "application/rtf";


break;


case "HTML40" :


—these are the options..has to specially set for HTML file export


tempFileName = Session.SessionID.ToString() + "." + "htm";


contentType = "text/html";


eft = ExportFormatType.HTML32;


HTMLFormatOptions hop = new HTMLFormatOptions();


hop.HTMLBaseFolderName = System.Configuration.ConfigurationSettings.AppSettings["TempDir"];


hop.HTMLFileName = Session.SessionID.ToString() + "." + "htm";


selectedReport.ExportOptions.FormatOptions = hop;


break;



}


—has to be done specially for HTML Files


if (eft == ExportFormatType.HTML32 || eft == ExportFormatType.HTML40)


{


string[] fp = selectedReport.FilePath.Split("\\".ToCharArray());


string leafDir = fp[fp.Length-1];


// strip .rpt extension


leafDir = leafDir.Substring(0, leafDir.Length - 4);


tempFileName = string.Format("{0}{1}\\{2}", System.Configuration.ConfigurationSettings.AppSettings["TempDir"], leafDir, tempFileName);


}


DiskFileDestinationOptions dfo = new CrystalDecisions.Shared.DiskFileDestinationOptions();


dfo.DiskFileName = tempFileName;


ExportOptions eo = selectedReport.ExportOptions;


eo.DestinationOptions = dfo;


eo.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;


eo.ExportFormatType = eft;


selectedReport.Export();


Response.ClearContent();


Response.ClearHeaders();


Response.Buffer= true;


Response.ContentType = contentType;


Response.AddHeader("Content-Disposition", "attachment;filename=" + tempFileName);


Response.WriteFile(tempFileName);


Response.Flush();


Response.Close();


System.IO.File.Delete(tempFileName);


}


catch(Exception ex)


{


Response.Write(ex.Message);


}


}


 

Post a Comment