C# Code Listings
The C# code listings are equivalent to the VB.NET code listings.
Dim myReport As New CrystalReport1()
'Export the report to the destination type ReportExport.PDF
SetDestinationDisk(myReport, "C:\ReportExport.PDF")
'Set the format to be PDF and only export pages 1-3
SetFormatPdfRtfWord(myReport, False, 1, 3)
'Perform the export
myReport.Export()
public void SetDiskFileDestination(
CrystalDecisions.CrystalReports.Engine.ReportDocument Report, string FileName)
{
//Set the destination type to DiskFile
Report.ExportOptions.ExportDestinationType =
CrystalDecisions.Shared.ExportDestinationType.DiskFile;
//Instantiate a DiskFileDestinationOptions object and set its
//FileName property
CrystalDecisions.Shared.DiskFileDestinationOptions Options =
new CrystalDecisions.Shared.DiskFileDestinationOptions();
Options.DiskFileName = FileName;
//Assign the object to the report
Report.ExportOptions.DestinationOptions = Options;
}
public void SetEmailDestination(CrystalDecisions.CrystalReports.Engine.ReportDocument Report, string MailTo, string CCList, string Subject, string Message, string UserName,string Password)
{
//Set the destination type to EMail
Report.ExportOptions.ExportDestinationType =
CrystalDecisions.Shared.ExportDestinationType.MicrosoftMail;
//Instantiate an Email options object and set its properties
CrystalDecisions.Shared.MicrosoftMailDestinationOptions Options =
new CrystalDecisions.Shared.MicrosoftMailDestinationOptions();
Options.UserName = UserName;
Options.Password = Password;
Options.MailSubject = Subject;
Options.MailMessage = Message;
Options.MailToList = MailTo;
Options.MailCCList = CCList;
//Assign the options object to the report
Report.ExportOptions.DestinationOptions = Options;
}
public void SetDestinationExchangeFolder(CrystalDecisions.CrystalReports.Engine.
ReportDocument Report, string FolderPath, string Password, string Profile)
{
//Set the destination type to ExchangeFolder
Report.ExportOptions.ExportDestinationType =
CrystalDecisions.Shared.ExportDestinationType.ExchangeFolder;
//Instantiate an ExchangeFolder options object and set its properties
CrystalDecisions.Shared.ExchangeFolderDestinationOptions Options =
new CrystalDecisions.Shared.ExchangeFolderDestinationOptions();
Options.DestinationType =
CrystalDecisions.Shared.ExchangeDestinationType.ExchangePostDocMessage;
Options.FolderPath = FolderPath;
Options.Password = Password;
Options.Profile = Profile;
Report.ExportOptions.DestinationOptions = Options;
}
public void SetFormatPdfRtfWord(CrystalDecisions.CrystalReports.Engine.ReportDocument Report, Boolean UsePageRange, int FirstPageNumber, int LastPageNumber)
{
//Change the next line if you want the format to be RTF or Word
Report.ExportOptions.ExportFormatType =
CrystalDecisions.Shared.ExportFormatType.PortableDocFormat;
//The following lines stay the same regardless of formatting
//for PDF, RTF or Word
CrystalDecisions.Shared.PdfRtfWordFormatOptions Options =
new CrystalDecisions.Shared.PdfRtfWordFormatOptions ();
Options.UsePageRange = UsePageRange;
if (Options.UsePageRange) {
Options.FirstPageNumber = FirstPageNumber;
Options.LastPageNumber = LastPageNumber;
}
Report.ExportOptions.FormatOptions = Options;
}
public void SetFormatExcel(CrystalDecisions.CrystalReports.Engine.ReportDocument Report, Boolean UseConstantColumnWidth, int ColumnWidth, Boolean UseColumnHeadings )
{
Report.ExportOptions.ExportFormatType =
CrystalDecisions.Shared.ExportFormatType.Excel;
CrystalDecisions.Shared.ExcelFormatOptions Options =
new CrystalDecisions.Shared.ExcelFormatOptions ();
Options.ExcelUseConstantColumnWidth = UseConstantColumnWidth;
Options.ExcelConstantColumnWidth = ColumnWidth;
Options.ExcelTabHasColumnHeadings = UseColumnHeadings;
Report.ExportOptions.FormatOptions = Options;
}
public void SetFormatHTML(CrystalDecisions.CrystalReports.Engine.ReportDocument Report, Boolean EnableSeparatedPages, Boolean HasPageNavigator, string HTMLBaseFolderName, string HTMLFilename, Boolean UsePageRange, int FirstPageNumber, int LastPageNumber)
{
//Set the destination type to HTML
Report.ExportOptions.ExportFormatType =
CrystalDecisions.Shared.ExportFormatType.HTML40;
//Instantiate an options object and set its properties
CrystalDecisions.Shared.HTMLFormatOptions Options =
new CrystalDecisions.Shared.HTMLFormatOptions ();
Options.HTMLEnableSeparatedPages = EnableSeparatedPages;
Options.HTMLHasPageNavigator = HasPageNavigator;
Options.HTMLFileName = HTMLFilename;
Options.HTMLBaseFolderName = HTMLBaseFolderName;
Options.UsePageRange = UsePageRange;
if (Options.UsePageRange) {
Options.FirstPageNumber = FirstPageNumber;
Options.LastPageNumber = LastPageNumber;
}
Report.ExportOptions.FormatOptions = Options;
}
public void PrintToPdfWithFile(
CrystalDecisions.CrystalReports.Engine.ReportDocument MyReport, string FileName)
{
FileName = Request.PhysicalApplicationPath + Session.SessionID
+ "_" + FileName;
//Instantiate the object that controls where the file is exported to
CrystalDecisions.Shared.DiskFileDestinationOptions DestOptions =
new CrystalDecisions.Shared.DiskFileDestinationOptions();
DestOptions.DiskFileName = FileName;
//Set the Export Options
MyReport.ExportOptions.ExportFormatType =
CrystalDecisions.Shared.ExportFormatType.PortableDocFormat;
MyReport.ExportOptions.ExportDestinationType =
CrystalDecisions.Shared.ExportDestinationType.DiskFile;
MyReport.ExportOptions.DestinationOptions = DestOptions;
MyReport.Export();
//Display the PDF in the current browser window
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = @"application/pdf";
Response.WriteFile(FileName);
Response.Flush();
Response.Close();
System.IO.File.Delete(FileName);
}
public void PrintToPdfWithStream(
CrystalDecisions.CrystalReports.Engine.ReportDocument MyReport)
{
CrystalDecisions.Shared.ExportOptions MyExportOptions =
new CrystalDecisions.Shared.ExportOptions();
MyExportOptions.ExportFormatType =
CrystalDecisions.Shared.ExportFormatType.PortableDocFormat;
CrystalDecisions.Shared.ExportRequestContext MyExportRequestContext =
new CrystalDecisions.Shared.ExportRequestContext();
MyExportRequestContext.ExportInfo = MyExportOptions;
System.IO.Stream MyStream = null;
MyStream = MyReport.FormatEngine.ExportToStream(MyExportRequestContext);
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = @"application/pdf";
Byte[] MyBuffer = new Byte[MyStream.Length];
MyStream.Read(MyBuffer,0, (int)MyStream.Length);
Response.BinaryWrite(MyBuffer);
Response.End();
}