{"id":766,"date":"2008-01-04T19:58:12","date_gmt":"2008-01-05T02:58:12","guid":{"rendered":"http:\/\/members.crystalreportsbook.com\/crystal-reports-xi\/1918-c-code-listings\/"},"modified":"2008-01-04T20:01:08","modified_gmt":"2008-01-05T03:01:08","slug":"1918-c-code-listings","status":"publish","type":"post","link":"http:\/\/www.crystalreportsonlinetraining.com\/training\/1918-c-code-listings\/","title":{"rendered":"19.17 C# Code Listings"},"content":{"rendered":"<h1>C# Code Listings<\/h1>\n<p>The C# code listings are equivalent to the VB.NET code listings.<\/p>\n<p><code_caption>Listing 19-1. Export to a PDF disk file<\/code_caption><br \/>\n<code>Dim myReport As New CrystalReport1()<\/code><br \/>\n<code>'Export the report to the destination type ReportExport.PDF<\/code><br \/>\n<code>SetDestinationDisk(myReport, \"C:\\ReportExport.PDF\")<\/code><br \/>\n<code>'Set the format to be PDF and only export pages 1-3<\/code><br \/>\n<code>SetFormatPdfRtfWord(myReport, False, 1, 3)<\/code><br \/>\n<code>'Perform the export<\/code><br \/>\n<code>myReport.Export()<\/code><br \/>\n<code_caption>Listing 19-2. Setting the destination to a disk file<\/code_caption><br \/>\n<code>public void SetDiskFileDestination(<\/code><br \/>\n<code>CrystalDecisions.CrystalReports.Engine.ReportDocument Report, string FileName)<\/code><br \/>\n<code>{<\/code><br \/>\n<code>\/\/Set the destination type to DiskFile<\/code><br \/>\n<code>Report.ExportOptions.ExportDestinationType =<\/code><br \/>\n<code>CrystalDecisions.Shared.ExportDestinationType.DiskFile;<\/code><br \/>\n<code>\/\/Instantiate a DiskFileDestinationOptions object and set its<\/code><br \/>\n<code>\/\/FileName property<\/code><br \/>\n<code>CrystalDecisions.Shared.DiskFileDestinationOptions Options =<\/code><br \/>\n<code>new CrystalDecisions.Shared.DiskFileDestinationOptions();<\/code><br \/>\n<code>Options.DiskFileName = FileName;<\/code><br \/>\n<code>\/\/Assign the object to the report<\/code><br \/>\n<code>Report.ExportOptions.DestinationOptions = Options;<\/code><br \/>\n<code>}<\/code><br \/>\n<code_caption>Listing 19-3. Send a report as an attachment of an email.<\/code_caption><br \/>\n<code>public void SetEmailDestination(CrystalDecisions.CrystalReports.Engine.ReportDocument Report, string MailTo, string CCList, string Subject, string Message, string UserName,string Password)<\/code><br \/>\n<code>{<\/code><br \/>\n<code>\/\/Set the destination type to EMail<\/code><br \/>\n<code>Report.ExportOptions.ExportDestinationType =<\/code><br \/>\n<code>CrystalDecisions.Shared.ExportDestinationType.MicrosoftMail;<\/code><br \/>\n<code>\/\/Instantiate an Email options object and set its properties<\/code><br \/>\n<code>CrystalDecisions.Shared.MicrosoftMailDestinationOptions Options =<\/code><br \/>\n<code>new CrystalDecisions.Shared.MicrosoftMailDestinationOptions();<\/code><br \/>\n<code>Options.UserName = UserName;<\/code><br \/>\n<code>Options.Password = Password;<\/code><br \/>\n<code>Options.MailSubject = Subject;<\/code><br \/>\n<code>Options.MailMessage = Message;<\/code><br \/>\n<code>Options.MailToList = MailTo;<\/code><br \/>\n<code>Options.MailCCList = CCList;<\/code><br \/>\n<code>\/\/Assign the options object to the report<\/code><br \/>\n<code>Report.ExportOptions.DestinationOptions = Options;<\/code><br \/>\n<code>}<\/code><br \/>\n<code_caption>Listing 19-4. Export a report to an Exchange folder.<\/code_caption><br \/>\n<code>public void SetDestinationExchangeFolder(CrystalDecisions.CrystalReports.Engine.<\/code><br \/>\n<code>ReportDocument Report, string FolderPath, string Password, string Profile)<\/code><br \/>\n<code>{<\/code><br \/>\n<code>\/\/Set the destination type to ExchangeFolder<\/code><br \/>\n<code>Report.ExportOptions.ExportDestinationType =<\/code><br \/>\n<code>CrystalDecisions.Shared.ExportDestinationType.ExchangeFolder;<\/code><br \/>\n<code>\/\/Instantiate an ExchangeFolder options object and set its properties<\/code><br \/>\n<code>CrystalDecisions.Shared.ExchangeFolderDestinationOptions Options =<\/code><br \/>\n<code>new CrystalDecisions.Shared.ExchangeFolderDestinationOptions();<\/code><br \/>\n<code>Options.DestinationType =<\/code><br \/>\n<code>CrystalDecisions.Shared.ExchangeDestinationType.ExchangePostDocMessage;<\/code><br \/>\n<code>Options.FolderPath = FolderPath;<\/code><br \/>\n<code>Options.Password = Password;<\/code><br \/>\n<code>Options.Profile = Profile;<\/code><br \/>\n<code>Report.ExportOptions.DestinationOptions = Options;<\/code><br \/>\n<code>}<\/code><br \/>\n<code_caption>Listing 19-5. Set the export formatting to PDF<\/code_caption><br \/>\n<code>public void SetFormatPdfRtfWord(CrystalDecisions.CrystalReports.Engine.ReportDocument Report, Boolean UsePageRange, int FirstPageNumber, int LastPageNumber)<\/code><br \/>\n<code>{<\/code><br \/>\n<code>\/\/Change the next line if you want the format to be RTF or Word<\/code><br \/>\n<code>Report.ExportOptions.ExportFormatType =<\/code><br \/>\n<code>CrystalDecisions.Shared.ExportFormatType.PortableDocFormat;<\/code><br \/>\n<code>\/\/The following lines stay the same regardless of formatting<\/code><br \/>\n<code>\/\/for PDF, RTF or Word<\/code><br \/>\n<code>CrystalDecisions.Shared.PdfRtfWordFormatOptions Options =<\/code><br \/>\n<code>new CrystalDecisions.Shared.PdfRtfWordFormatOptions ();<\/code><br \/>\n<code>Options.UsePageRange = UsePageRange;<\/code><br \/>\n<code>if (Options.UsePageRange) {<\/code><br \/>\n<code>Options.FirstPageNumber = FirstPageNumber;<\/code><br \/>\n<code>Options.LastPageNumber = LastPageNumber;<\/code><br \/>\n<code>}<\/code><br \/>\n<code>Report.ExportOptions.FormatOptions = Options;<\/code><br \/>\n<code>}<\/code><br \/>\n<code_caption>Listing 19-6. Setting the format to be an Excel spreadsheet.<\/code_caption><br \/>\n<code>public void SetFormatExcel(CrystalDecisions.CrystalReports.Engine.ReportDocument Report, Boolean UseConstantColumnWidth, int ColumnWidth, Boolean UseColumnHeadings )<\/code><br \/>\n<code>{<\/code><br \/>\n<code>Report.ExportOptions.ExportFormatType =<\/code><br \/>\n<code>CrystalDecisions.Shared.ExportFormatType.Excel;<\/code><br \/>\n<code>CrystalDecisions.Shared.ExcelFormatOptions Options =<\/code><br \/>\n<code>new CrystalDecisions.Shared.ExcelFormatOptions ();<\/code><br \/>\n<code>Options.ExcelUseConstantColumnWidth = UseConstantColumnWidth;<\/code><br \/>\n<code>Options.ExcelConstantColumnWidth = ColumnWidth;<\/code><br \/>\n<code>Options.ExcelTabHasColumnHeadings = UseColumnHeadings;<\/code><br \/>\n<code>Report.ExportOptions.FormatOptions = Options;<\/code><br \/>\n<code>}<\/code><br \/>\n<code_caption>Listing 19-7. Setting the format options for HTML.<\/code_caption><br \/>\n<code>public void SetFormatHTML(CrystalDecisions.CrystalReports.Engine.ReportDocument Report, Boolean EnableSeparatedPages, Boolean HasPageNavigator, string HTMLBaseFolderName, string HTMLFilename, Boolean UsePageRange, int FirstPageNumber, int LastPageNumber)<\/code><br \/>\n<code>{<\/code><br \/>\n<code>\/\/Set the destination type to HTML<\/code><br \/>\n<code>Report.ExportOptions.ExportFormatType =<\/code><br \/>\n<code>CrystalDecisions.Shared.ExportFormatType.HTML40;<\/code><br \/>\n<code>\/\/Instantiate an options object and set its properties<\/code><br \/>\n<code>CrystalDecisions.Shared.HTMLFormatOptions Options =<\/code><br \/>\n<code>new CrystalDecisions.Shared.HTMLFormatOptions ();<\/code><br \/>\n<code>Options.HTMLEnableSeparatedPages = EnableSeparatedPages;<\/code><br \/>\n<code>Options.HTMLHasPageNavigator = HasPageNavigator;<\/code><br \/>\n<code>Options.HTMLFileName = HTMLFilename;<\/code><br \/>\n<code>Options.HTMLBaseFolderName = HTMLBaseFolderName;<\/code><br \/>\n<code>Options.UsePageRange = UsePageRange;<\/code><br \/>\n<code>if (Options.UsePageRange) {<\/code><br \/>\n<code>Options.FirstPageNumber = FirstPageNumber;<\/code><br \/>\n<code>Options.LastPageNumber = LastPageNumber;<\/code><br \/>\n<code>}<\/code><br \/>\n<code>Report.ExportOptions.FormatOptions = Options;<\/code><br \/>\n<code>}<\/code><br \/>\n<code_caption>Listing 19-8. Print from ASP.NET using a PDF file.<\/code_caption><br \/>\n<code>public void PrintToPdfWithFile(<\/code><br \/>\n<code>CrystalDecisions.CrystalReports.Engine.ReportDocument MyReport, string FileName)<\/code><br \/>\n<code>{<\/code><br \/>\n<code>FileName = Request.PhysicalApplicationPath + Session.SessionID<\/code><br \/>\n<code>+ \"_\" + FileName;<\/code><br \/>\n<code>\/\/Instantiate the object that controls where the file is exported to<\/code><br \/>\n<code>CrystalDecisions.Shared.DiskFileDestinationOptions DestOptions =<\/code><br \/>\n<code>new CrystalDecisions.Shared.DiskFileDestinationOptions();<\/code><br \/>\n<code>DestOptions.DiskFileName = FileName;<\/code><br \/>\n<code>\/\/Set the Export Options<\/code><br \/>\n<code>MyReport.ExportOptions.ExportFormatType =<\/code><br \/>\n<code>CrystalDecisions.Shared.ExportFormatType.PortableDocFormat;<\/code><br \/>\n<code>MyReport.ExportOptions.ExportDestinationType =<\/code><br \/>\n<code>CrystalDecisions.Shared.ExportDestinationType.DiskFile;<\/code><br \/>\n<code>MyReport.ExportOptions.DestinationOptions = DestOptions;<\/code><br \/>\n<code>MyReport.Export();<\/code><br \/>\n<code>\/\/Display the PDF in the current browser window<\/code><br \/>\n<code>Response.ClearContent();<\/code><br \/>\n<code>Response.ClearHeaders();<\/code><br \/>\n<code>Response.ContentType = @\"application\/pdf\";<\/code><br \/>\n<code>Response.WriteFile(FileName);<\/code><br \/>\n<code>Response.Flush();<\/code><br \/>\n<code>Response.Close();<\/code><br \/>\n<code>System.IO.File.Delete(FileName);<\/code><br \/>\n<code>}<\/code><br \/>\n<code_caption>Listing 19-9. Export to PDF using a binary stream.<\/code_caption><br \/>\n<code>public void PrintToPdfWithStream(<\/code><br \/>\n<code>CrystalDecisions.CrystalReports.Engine.ReportDocument MyReport)<\/code><br \/>\n<code>{<\/code><br \/>\n<code>CrystalDecisions.Shared.ExportOptions MyExportOptions =<\/code><br \/>\n<code>new CrystalDecisions.Shared.ExportOptions();<\/code><br \/>\n<code>MyExportOptions.ExportFormatType =<\/code><br \/>\n<code>CrystalDecisions.Shared.ExportFormatType.PortableDocFormat;<\/code><br \/>\n<code>CrystalDecisions.Shared.ExportRequestContext MyExportRequestContext =<\/code><br \/>\n<code>new CrystalDecisions.Shared.ExportRequestContext();<\/code><br \/>\n<code>MyExportRequestContext.ExportInfo = MyExportOptions;<\/code><br \/>\n<code>System.IO.Stream MyStream = null;<\/code><br \/>\n<code>MyStream = MyReport.FormatEngine.ExportToStream(MyExportRequestContext);<\/code><br \/>\n<code>Response.ClearContent();<\/code><br \/>\n<code>Response.ClearHeaders();<\/code><br \/>\n<code>Response.ContentType = @\"application\/pdf\";<\/code><br \/>\n<code>Byte[] MyBuffer = new Byte[MyStream.Length];<\/code><br \/>\n<code>MyStream.Read(MyBuffer,0, (int)MyStream.Length);<\/code><br \/>\n<code>Response.BinaryWrite(MyBuffer);<\/code><br \/>\n<code>Response.End();<\/code><br \/>\n<code>}<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>C# Code Listings The C# code listings are equivalent to the VB.NET code listings. Listing 19-1. Export to a PDF disk file Dim myReport As New CrystalReport1() &#8216;Export the report to the destination type ReportExport.PDF SetDestinationDisk(myReport, &#8220;C:\\ReportExport.PDF&#8221;) &#8216;Set the format to be PDF and only export pages 1-3 SetFormatPdfRtfWord(myReport, False, 1, 3) &#8216;Perform the export [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[39,2],"tags":[],"class_list":["post-766","post","type-post","status-publish","format-standard","hentry","category-chapter-19-exporting-and-deploying-reports","category-crystal-reportsnet-2003","entry"],"_links":{"self":[{"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/posts\/766","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/comments?post=766"}],"version-history":[{"count":0,"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/posts\/766\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/media?parent=766"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/categories?post=766"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/tags?post=766"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}