public static bool ExportReport(ReportDocument crReportDocument, string ExpType,string ExportPath, string filename)
{
//creating full report file name for example if the filename was "MyReport1"
//and ExpType was "pdf", full file name will be "MyReport1.pdf"
filename = filename + "." + ExpType;
//creating storage directory if not exists
if (!Directory.Exists(ExportPath))
Directory.CreateDirectory(ExportPath);
//creating new instance representing disk file destination options such as filename, export type etc.
DiskFileDestinationOptions crDiskFileDestinationOptions = new DiskFileDestinationOptions();
ExportOptions crExportOptions = crReportDocument.ExportOptions;
switch(ExpType)
{
case "rtf":
{
//setting disk file name
crDiskFileDestinationOptions.DiskFileName = ExportPath + filename;
//setting destination type in our case disk file
crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
//setuing export format type
crExportOptions.ExportFormatType = ExportFormatType.RichText;
//setting previously defined destination opions to our input report document
crExportOptions.DestinationOptions = crDiskFileDestinationOptions;
break;
}
//NOTE following code is similar to previous, so I want comment it again
case "pdf":
{
crDiskFileDestinationOptions.DiskFileName = ExportPath + filename;
crExportOptions.DestinationOptions = crDiskFileDestinationOptions;
crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
crExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
break;
}
case "doc":
{
crDiskFileDestinationOptions.DiskFileName = ExportPath + filename;
crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
crExportOptions.ExportFormatType = ExportFormatType.WordForWindows;
crExportOptions.DestinationOptions = crDiskFileDestinationOptions;
break;
}
case "xls":
{
crDiskFileDestinationOptions.DiskFileName = ExportPath + filename;
crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
crExportOptions.ExportFormatType = ExportFormatType.Excel;
crExportOptions.DestinationOptions = crDiskFileDestinationOptions;
break;
}
case "rpt":
{
crDiskFileDestinationOptions.DiskFileName = ExportPath + filename;
crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
crExportOptions.ExportFormatType = ExportFormatType.CrystalReport;
crExportOptions.DestinationOptions = crDiskFileDestinationOptions;
break;
}
case "htm":
{
HTMLFormatOptions HTML40Formatopts = new HTMLFormatOptions();
crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
crExportOptions.ExportFormatType = ExportFormatType.HTML40;
HTML40Formatopts.HTMLBaseFolderName = ExportPath + filename;
HTML40Formatopts.HTMLFileName = "HTML40.html";
HTML40Formatopts.HTMLEnableSeparatedPages = true;
HTML40Formatopts.HTMLHasPageNavigator = true;
HTML40Formatopts.FirstPageNumber = 1;
HTML40Formatopts.LastPageNumber = 3;
crExportOptions.FormatOptions = HTML40Formatopts;
break;
}
}
try
{
//trying to export input report document, and if success returns true
crReportDocument.Export();
return true;
}
catch (Exception e)
{
throw e;
}
}
///
/// Export report to byte array
///
/// ReportDocument
/// CrystalDecisions.Shared.ExportFormatType
///
public static byte[] ExportReportToStream(ReportDocument crReportDocument,ExportFormatType exptype)
{//this code exports input report document into stream, and returns array of bytes
Stream st;
st = crReportDocument.ExportToStream(exptype);
byte[] arr = new byte[st.Length];
st.Read(arr,0,(int) st.Length);
return arr;
}
///
/// Export report to string
///
/// ReportDocument
///
public static string ExportReportToString(ReportDocument crReportDocument)
{
Stream st;
st = crReportDocument.ExportToStream(ExportFormatType.PortableDocFormat);
byte[] arr = new byte[st.Length];
st.Read(arr,0,(int) st.Length);
string rep = new UnicodeEncoding().GetString(arr);
return rep;
}
August 26, 2004
Export Report Utility
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment