private void exportReport(CrystalDecisions.CrystalReports.Engine.ReportClass selectedReport, CrystalDecisions.Shared.ExportFormatType eft) { selectedReport.ExportOptions.ExportFormatType = eft; string contentType = ""; // Make sure asp.net has create and delete permissions in the directory string tempDir = System.Configuration.ConfigurationSettings.AppSettings["TempDir"]; string tempFileName = Session.SessionID.ToString() + "."; switch (eft) { case CrystalDecisions.Shared.ExportFormatType.PortableDocFormat: tempFileName += "pdf"; contentType = "application/pdf"; break; case CrystalDecisions.Shared.ExportFormatType.WordForWindows: tempFileName += "doc"; contentType = "application/msword"; break; case CrystalDecisions.Shared.ExportFormatType.Excel: tempFileName += "xls"; contentType = "application/vnd.ms-excel"; break; case CrystalDecisions.Shared.ExportFormatType.HTML32: case CrystalDecisions.Shared.ExportFormatType.HTML40: tempFileName += "htm"; contentType = "text/html"; CrystalDecisions.Shared.HTMLFormatOptions hop = new CrystalDecisions.Shared.HTMLFormatOptions(); hop.HTMLBaseFolderName = tempDir; hop.HTMLFileName = tempFileName; selectedReport.ExportOptions.FormatOptions = hop; break; } CrystalDecisions.Shared.DiskFileDestinationOptions dfo = new CrystalDecisions.Shared.DiskFileDestinationOptions(); dfo.DiskFileName = tempDir + tempFileName; selectedReport.ExportOptions.DestinationOptions = dfo; selectedReport.ExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile; selectedReport.Export(); selectedReport.Close(); string tempFileNameUsed; //if (eft == CrystalDecisions.Shared.ExportFormatType.HTML32 || eft == CrystalDecisions.Shared.ExportFormatType.HTML40) //{ // string[] fp = selectedReport.FilePath.Split("\\".ToCharArray()); // string leafDir = fp[fp.Length - 1]; // // strip .rpt extension // leafDir = leafDir.Substring(0, leafDir.Length–4); // tempFileNameUsed = string.Format("{0}{1}\\{2}", tempDir, leafDir, tempFileName); //} //else tempFileNameUsed = tempDir + tempFileName; Response.ClearContent(); Response.ClearHeaders(); Response.ContentType = contentType; Response.WriteFile(tempFileNameUsed); Response.Flush(); Response.Close(); System.IO.File.Delete(tempFileNameUsed); }
public static string GenerateExport(CrystalDecisions.CrystalReports.Engine.ReportDocument selectedReport, CrystalDecisions.Shared.ExportFormatType eft, string ExportFileName) { selectedReport.ExportOptions.ExportFormatType = eft; string contentType = ""; // Make sure asp.net has create and delete permissions in the directory string tempDir = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["tempDir"]); string tempFileName = string.Concat(Path.GetFileNameWithoutExtension(selectedReport.FileName), "."); if (! string.IsNullOrEmpty(ExportFileName)) { tempFileName = string.Concat(ExportFileName, "."); } switch (eft) { case CrystalDecisions.Shared.ExportFormatType.PortableDocFormat: tempFileName += "pdf"; break; case CrystalDecisions.Shared.ExportFormatType.WordForWindows: tempFileName += "doc"; break; case CrystalDecisions.Shared.ExportFormatType.Excel: tempFileName += "xls"; break; case CrystalDecisions.Shared.ExportFormatType.HTML32: case CrystalDecisions.Shared.ExportFormatType.HTML40: tempFileName += "htm"; CrystalDecisions.Shared.HTMLFormatOptions hop = new CrystalDecisions.Shared.HTMLFormatOptions(); hop.HTMLBaseFolderName = tempDir; hop.HTMLFileName = tempFileName; selectedReport.ExportOptions.FormatOptions = hop; break; } CrystalDecisions.Shared.DiskFileDestinationOptions dfo = new CrystalDecisions.Shared.DiskFileDestinationOptions(); dfo.DiskFileName = Path.Combine(tempDir, tempFileName); selectedReport.ExportOptions.DestinationOptions = dfo; selectedReport.ExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile; selectedReport.Export(); selectedReport.Close(); string tempFileNameUsed; if (eft == CrystalDecisions.Shared.ExportFormatType.HTML32 || eft == CrystalDecisions.Shared.ExportFormatType.HTML40) { string[] fp = selectedReport.FilePath.Split("\\" .ToCharArray()); string leafDir = fp[fp.Length - 1]; // strip .rpt extension leafDir = leafDir.Substring(0, leafDir.Length - 4); tempFileNameUsed = string.Format("{0}{1}\\{2}", tempDir, leafDir, tempFileName); } else { tempFileNameUsed = Path.Combine(tempDir, tempFileName); } return tempFileNameUsed; }