Electronic Report Delivery


In the previous section, you used a browser to display the Crystal Reports ReportDocument. Believe it or not, that is a type of report delivery. Yes, it's a stretch to define it as such. However, if you consider that an end user is able to print the displayed HTML from the browser's toolbar (by selecting File Print or pressing Ctrl-P), it's not that difficult to consider the browser display as a type of report delivery.

Certainly , this is better than loading a ton of reports into a delivery van for offsite users. Using your ASP.NET Web application, you can easily "deliver" reports across the world, just like that. This, of course, opens the floodgates for value- added enhancements. In the sections that follow, I discuss the following types of report delivery enhancements:

  • Export format options

  • E-mail delivery

  • XML Web services

Export Format Options

Crystal Reports for VS .NET is flexible and supports various export options. To meet the various demands and expectations of the modern-day end user, you can use Crystal Reports for VS .NET to export reports in a variety of formats. Among the most popular "report" formats to export are

  • Microsoft Word documents (.doc)

  • Microsoft Excel spreadsheets (.xls)

  • Adobe Acrobat Portable Document Format (PDF) files (.pdf)

To take advantage of Crystal Reports's exporting feature, you will need to familiarize yourself with the Crystal Reports class library (discussed earlier in section "The Crystal Reports Class Library"). Specifically, you need to use the following three namespaces:

  • CrystalDecisions.CrystalReports.Engine

  • CrystalDecisions.Shared

  • CrystalDecisions.Web

I have modified the ASP.NET Web sample project CrystalReportsExampleVB to add exporting features. Two new Web Forms (Webform2.aspx and Webform3.aspx) and several Web server controls have been added to the project to assist with navigating back and forth between each Web Form (see Figure 16-14).

click to expand
Figure 16-14: The ASP.NET Web sample project CrystalReportsExampleVB with a few enhancements added to provide navigation between each Web Form. WebForm2.aspx will be used to add "export" logic.

In the Click event/method for several Web server buttons on WebForm2.aspx, a small amount of code has been added to accomplish an "export." Please review the code snippets in Listings 16-1 through 16-3.

Listing 16-1: Code to Export a Crystal Reports ReportDocument in Microsoft Word (.doc) Format
start example
 Private Sub Button2_Click(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles Button2.Click              Dim CrReport As New _          CrystalDecisions.CrystalReports.Engine.ReportDocument()          CrReport.Load(Server.MapPath("CrystalReport1.rpt"))          Dim CrExportOptions As CrystalDecisions.Shared.ExportOptions          Dim CrDiskFileDestinationOptions As New _          CrystalDecisions.Shared.DiskFileDestinationOptions()          Dim CrFormatTypeOptions As New _          CrystalDecisions.Shared.PdfRtfWordFormatOptions()              CrDiskFileDestinationOptions.DiskFileName = _          Server.MapPath("myfirstCR.doc")          CrFormatTypeOptions.FirstPageNumber = 1          CrFormatTypeOptions.LastPageNumber = 2          CrFormatTypeOptions.UsePageRange = True          CrExportOptions = CrReport.ExportOptions              With CrExportOptions                  .ExportDestinationType = _                  CrystalDecisions.Shared.ExportDestinationType.DiskFile                  .ExportFormatType = _                  CrystalDecisions.Shared.ExportFormatType.WordForWindows                  .DestinationOptions = CrDiskFileDestinationOptions                  .FormatOptions = CrFormatTypeOptions          End With          CrReport.Export()      End Sub 
end example
 
Listing 16-2: Code to Export a Crystal Reports ReportDocument in Adobe Acrobat PDF (.pdf) Format
start example
 Private Sub Button3_Click(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles Button3.Click             Dim CrReport As New _         CrystalDecisions.CrystalReports.Engine.ReportDocument()         CrReport.Load(Server.MapPath("CrystalReport1.rpt"))         Dim CrExportOptions As CrystalDecisions.Shared.ExportOptions         Dim CrDiskFileDestinationOptions As New _         CrystalDecisions.Shared.DiskFileDestinationOptions()         Dim CrFormatTypeOptions As New _         CrystalDecisions.Shared.PdfRtfWordFormatOptions()             CrDiskFileDestinationOptions.DiskFileName = _         Server.MapPath("myfirstCR.pdf")         CrFormatTypeOptions.FirstPageNumber = 1         CrFormatTypeOptions.LastPageNumber = 2         CrFormatTypeOptions.UsePageRange = True         CrExportOptions = CrReport.ExportOptions             With CrExportOptions                 .ExportDestinationType = _                 CrystalDecisions.Shared.ExportDestinationType.DiskFile                 .ExportFormatType = _                 CrystalDecisions.Shared.ExportFormatType.PortableDocFormat                 .DestinationOptions = CrDiskFileDestinationOptions                 .FormatOptions = CrFormatTypeOptions         End With         CrReport.Export()      End Sub 
end example
 
Listing 16-3: Code to Export a Crystal Reports ReportDocument in Microsoft Excel
(.xls) Format
start example
 Private Sub Button4_Click(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles Button4.Click             Dim CrReport As New _         CrystalDecisions.CrystalReports.Engine.ReportDocument()         CrReport.Load(Server.MapPath("CrystalReport1.rpt"))         Dim CrExportOptions As CrystalDecisions.Shared.ExportOptions         Dim CrDiskFileDestinationOptions As New _         CrystalDecisions.Shared.DiskFileDestinationOptions()         Dim CrFormatTypeOptions As New _         CrystalDecisions.Shared.ExcelFormatOptions()             CrDiskFileDestinationOptions.DiskFileName = _         Server.MapPath("myfirstCR.xls")         CrFormatTypeOptions.ExcelTabHasColumnHeadings = True         CrFormatTypeOptions.ExcelUseConstantColumnWidth = False         CrExportOptions = CrReport.ExportOptions          With CrExportOptions                 .ExportDestinationType = _                 CrystalDecisions.Shared.ExportDestinationType.DiskFile                 .ExportFormatType = _                  CrystalDecisions.Shared.ExportFormatType.Excel                 .DestinationOptions = CrDiskFileDestinationOptions                 .FormatOptions = CrFormatTypeOptions         End With         CrReport.Export()      End Sub 
end example
 

In Listings 16-1 through 16-3, you will notice very subtle differences between the syntax required to export each report format. Please take a moment to review the code. Feel free to experiment with the many available options. The Crystal Reports class library will be a helpful source when you want to know the additional options available to further customize each report format. As always, references are provided at the end of this chapter in the "To Learn More" section to assist in your continued retraining efforts.

Tip  

In your actual applications, you will want to add exception handling logic. Additionally, you will typically rename your server controls, Web Forms, report files, and so forth from their default names to more functional and descriptive names .

Run the sample application and export each report format. As shown in Figure 16-15, each report format export will result in a file being created. In a real-world Web application, you would definitely have chosen to create an IIS virtual directory for the destination of the reports (I further discuss the creation of IIS virtual directories in Chapter 17) instead of exposing your application folder.

click to expand
Figure 16-15: Using Windows Explorer outside of the VS .NET IDE to verify the creation of the exported report formats

As you can see in Figure 16-16, I have added a few HREF controls to WebForm3.aspx (of the CrystalReportsExampleVB sample project) to view the contents of each exported file format. In a production application, the HREF would typically contain the URL that points back to an IIS virtual directory (as noted earlier, I discuss the creation of IIS virtual directories in Chapter 17) instead of using the approach shown in this sample application.

click to expand
Figure 16-16: WebForm3.aspx of the CrystalReportsExampleVB sample project with a few HREF controls added
Tip  

You will need to have Adobe Acrobat Reader software installed to view files exported in the Adobe Acrobat PDF (.pdf) format. The Adobe Acrobat Reader software is available as a free download from the Adobe Web site ( http://www.adobe.com/ ).

In modern-day reporting, multiple formats are delivered right to the browser. You have got to admit, this is really great. Once the desired format is electronically delivered to the end user, he or she is free to print the report. Well, it only gets better. Suppose you want to use e-mail as a delivery vehicle. It is certainly possible. In the next section, you will explore the e-mail alternative to report distribution.

start sidebar
Windows Forms Controls for Printing

In the event that you are creating a .NET Windows application and wish to add "normal" printing capability to your application, you are in luck. .NET provides the following four classes that you will want to further explore:

  • PrintPreview: System.Windows.Forms.PrintPreviewControl

  • PrintDialog: System.Windows.Forms.PrintDialog

  • PageSetup: System.Windows.Forms.PageSetupDialog

  • PrintDocument: System.Drawing.Printing.PrintDocument

As you use these .NET Framework classes, you will be reminded of working with the mainframe COBOL Report Writer feature ”except I believe the .NET Frame-work classes are more developer-friendly.

end sidebar
 

E-mail with SMTP

As you certainly know, the use of e-mail is very popular. This is a good thing. As a developer, you can take advantage of e-mail as a report delivery vehicle. The .NET platform provides a set of managed classes to support sending e-mail using the Collaboration Data Objects for Windows 2000 (CDOSYS) message component. This set of classes is provided via the System.Web.Mail namespace.

Cross-Reference  

The CDOSYS message component was originally discussed in Chapter 10.

Before I get into the code needed to implement an SMTP e-mail delivery solution, I need to mention a couple of points:

  • You will need the SMTP service installed on your IIS Web server in order to send e-mail from your Web application using the SMTP managed classes.

  • If your organization happens to use Microsoft Exchange Server for e-mail relaying, you will need to properly connect and configure your installed SMTP service to the Exchange Server. With this type of setup, you will need appropriate access permissions to the Exchange Server to complete your SMTP configuration. However, it is possible to send e- mails using SMTP without the use of Exchange Server, assuming that SMTP and your Internet connection are properly configured.

Cross-Reference  

SMTP was originally introduced in Chapter 3, and Exchange Server was originally introduced in Chapter 10.

Note  

The setup and configuration of the SMTP service and/or Exchange Server are beyond the scope of this text. However, I provide several references at the end of this chapter in the "To Learn More" section to point you in the right direction. Otherwise, your organization's network administration group should be able to provide further assistance.

As shown in Figure 16-17, the SMTP service will appear in your Computer Management console if it is installed as part of your IIS Web server installation.

click to expand
Figure 16-17: The Computer Management console showing the SMTP service installed

By the way, if you have SMTP set up locally without Exchange Server, your sent e-mail will simply sit in the SMTP queue waiting for delivery. That is OK for now. At least you can verify that your programming task was completed. An SMTP instal lation will include the default setup of a "mailroot" folder structure as shown in Figure 16-18. As you can see in Figure 16-19, an Internet e-mail message is easily identifiable in the SMTP queue.


Figure 16-18: The mailroot folder structure for SMTP e-mail support
click to expand
Figure 16-19: An Internet e-mail message in the SMTP queue

Now that that's out of the way, you'll take a look at how simple the actual pro gramming is for SMTP e-mail. Further enhancing the CrystalReportsExampleVB sample project, I've added a few server controls that will facilitate the attempted send of the e-mail using SMTP. Listing 16-4 demonstrates how to send the report files (previously exported) using the SMTP e-mail approach.

Listing 16-4: Sending E-mail Attachments with System.Web.Mail Managed Classes and SMTP
start example
 Private Sub Button3_Click(ByVal sender As System.Object, _       ByVal e As System.EventArgs) Handles Button3.Click           Dim MyEmailMessage As New System.Web.Mail.MailMessage()       MyEmailMessage.To = TextBox1.Text       MyEmailMessage.From = TextBox1.Text       MyEmailMessage.Subject = "A Satisfied Customer"       MyEmailMessage.BodyFormat = System.Web.Mail.MailFormat.Text       MyEmailMessage.Body = "Sending an SMTP Email is easy!"           Dim myattachment1 As System.Web.Mail.MailAttachment = New _       System.Web.Mail.MailAttachment(Server.MapPath("myfirstCR.doc"))       MyEmailMessage.Attachments.Add(myattachment1)           Dim myattachment2 As System.Web.Mail.MailAttachment = New _       System.Web.Mail.MailAttachment(Server.MapPath("myfirstCR.pdf"))       MyEmailMessage.Attachments.Add(myattachment2)           Dim myattachment3 As System.Web.Mail.MailAttachment = New _       System.Web.Mail.MailAttachment(Server.MapPath("myfirstCR.xls"))       MyEmailMessage.Attachments.Add(myattachment3)           System.Web.Mail.SmtpMail.Send(MyEmailMessage)     End Sub 
end example
 
Note  

If your assembly does not have the appropriate security permissions, you may experience an exception/error message when you execute the SMTP Send method. As discussed in the earlier section "Potential Exception Messages," there are solutions for this type of problem.

As you can see from Listing 16-4, the logic is rather simple. The support pro vided by .NET via the System.Web.Mail namespace makes sending e-mail a viable report delivery option. In the next section, you will take a look at another report delivery approach. Having options is really a great characteristic of the .NET platform that enables you to pick the solution that best fits your business needs.

start sidebar
What About the HTML Mailto URL?

You can also send e-mail using a simple HTML mailto URL. Using either the <a> HTML anchor element or the HyperLink Web server control, the HREF attribute value can be set to an e-mail address. This approach to sending e-mail is different from the SMTP approach. It is much simpler and you do not need an SMTP server or Exchange Server setup. You simply need to be connected to the Internet.

However, this simpler approach does not provide the rich .NET managed object module that enables value-added features such as adding attachments. The CrystalReportsExampleVB sample project has a HyperLink Web server control added to WebForm3.aspx that will send an e-mail for demonstration purposes.

end sidebar
 

XML Web Services

The Crystal Reports ReportDocument report file (.rpt) has the ability to be con verted to an XML Web service. The process to do this is so easy it is amazing. The scenario would simply be that you want to expose your report as XML via the Internet. In other words, you can use this feature as yet another way to deliver your report to the end user. The end user, in this case, would use an application (e.g., a Windows or ASP.NET application) to consume the XML.

Cross-Reference  

I covered the topic of XML Web services in Chapter 13 in the section "Understanding the ASP.NET Web Service Technology."

The two Crystal Reports class library namespaces you use during this process are CrystalDecisions.Web.Services and CrystalDecisions.Web.Services.Enterprise. Let's now revisit the sample application to review the simple technique involved in converting the Crystal ReportDocument report file to an XML Web service.

As shown in Figure 16-20, you need to select the Crystal ReportDocument report file in Solution Explorer. Then, you'll want to right-click and select "Publish as web service" from the drop-down context menu. That's it!

click to expand
Figure 16-20: Publishing the Crystal ReportDocument report file as a Web service

Notice that when you complete these few steps, an .asmx file appears in the VS .NET Solution Explorer window (see Figure 16-21). In the case of the sample appli cation, the new file is named CrystalReport1Service.asmx. Basically, that is your XML Web service.

click to expand
Figure 16-21: Viewing the VS .NET Solution Explorer window showing the .asmx file for the new XML Web service

Testing the XML Web Service

The easiest way to test your XML Web service is to navigate directly to the .asmx file. You can do this by typing the appropriate address (i.e., http://localhost/CrystalReportsExampleVB/CrystalReport1Service.asmx ) into your browser address bar. Optionally, you can modify the CrystalReport1Service.asmx file by right-clicking and selecting Set As Start Page. Then, when you start the sample project, the automatic XML Web service testing harness will display (see Figure 16-22).

click to expand
Figure 16-22: The CrystalReport1Service.asmx file as seen in the automatic XML Web service testing harness

You'll need to customize the CrystalReport1Service.asmx.vb code-behind file to fully enable the testing ability of the testing harness. In fact, you can customize the automatic XML Web service testing harness in virtually any other way you see fit. For example, you can modify the entry "banner page" to display your company's name . Additionally, for confidentiality reasons, you may choose to sup press some of functionality that's exposed by default.

Feel free to experiment with the sample application to continue your retraining effort. You might even create a separate .NET application (using COBOL .NET or VB .NET, Windows or ASP.NET) to serve as a consuming client application. In other words, you can create an application that makes use of the VS .NET Solution Explorer's Add Web Reference feature. This client application would then "consume" the XML returned from the XML Web service. The references provided in the "To Learn More" section should be helpful to you as you continue your journey.




COBOL and Visual Basic on .NET
COBOL and Visual Basic on .NET: A Guide for the Reformed Mainframe Programmer
ISBN: 1590590481
EAN: 2147483647
Year: 2003
Pages: 204

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net