Chọn Cho dù các báo cáo sẽ được hiển thị, in, hoặc xuất khẩu Sử dụng Visual Basic NET Mã. Tôi biết tôi có thể sử dụng Crystal Report Viewer để in ấn và xuất khẩu báo cáo của tôi, nhưng tôi muốn để có thể có quyền kiểm soát trên đó, và có thể thậm chí không bao gồm các Viewer trong một số trường hợp
Nội dung trích xuất từ tài liệu:
Select Whether the Report Will Be Displayed, Printed, or Exported Using Visual Basic .NET 10.4 Select Whether the Report Will Be Displayed, Printed, or ExportedUsing Visual Basic .NET CodeI know I can use the Crystal Report Viewer to print and export my reports, but I want tobe able to have control over that, and maybe not even include the Viewer in some cases.TechniqueFor this How-To, you will use a Tab control to display the three options for the user:Print, Export, and View (see Figure 10.18). Figure 10.18. You have flexibility when it comes to letting your users work with reports.The first tab, Print, allows you to specify the number of copies to print, along with thestarting and ending pages.Printing Using the Report DocumentThis page will use the PrintToPrinter method shown here:Me.rdHowTo10_4.PrintToPrinter(Me.txtNumOfCopies.Text, False, Me.txtStartPage.Text, Me.txtEndPage.Text)The PrintOptions object, found on the ReportDocument, is very useful. The PrintOptionsobject has the following properties: PaperOrientation, PaperSize, PaperSource,PrinterDuplex, and PrinterName. You can set these properties either at design time usingthe property sheet, or at runtime using code.Exporting Using the Report DocumentWhen youre exporting using the Report document, you will be using the ExportOptionsobject. The ExportOptions object is made up of four other properties/objects: • DestinationOptions. This is made up of one of three possible objects: DiskFileDestinationOptions, ExchangeFolderDestinationOptions, or MicrosoftMailDestinationOptions. • ExportDestinationType. This gets or sets the export destination type. This will be DiskFile, ExchangeFolder, MicrosoftMail, or NoDestinationType. • ExportFormatType. This gets or sets the export format type. It can be one of the following: Excel, HTML32, HTML40, NoFormat, PortableDocFormat, RichText, or WordForWindows. • FormatOptions. This gets or sets the FormatOptions. It can be ExcelFormatOptions, HTMLFormatOptions, or PdfRtfWordFormatOptions.To execute the export, you use the Export method of the DocumentReport object. Youcan see the page for exporting in Figure 10.19. Figure 10.19. You can control which types of files you want the user to export.The View tab uses a CrystalReportViewer object on the tab page.StepsOpen and run the Visual Basic .NET-Chapter 10 solution. Click on the button labeledHow-To 10.4. Clicking on the tabs, you can see the three options you have to work with.Clicking on the Print button prints your report to the default printer. When you go to theExport tab and click the Export button, a message appears stating that the data has beenexported. The last tab, View, displays the report in a Viewer. 1. Create a Windows Form. Then place a Tab control on your form. 2. Add pages for Print, Export, and View, using the TabPages property of the Tab control. 3. Drag on a ReportDocument object, and set it to point to the report you created in How-To 10.1. Then name your report document rdHowTo10_4. 4. Place the controls shown in Figure 10.18 and 10.19 onto the tab page with the properties set forth in Table 10.3. Table 10.3. Controls for the Tab Pages Tab Page Object Property Setting Print Label Text # of Copies Label Text Start Page Label Text End Page TextBox Name txtNumOfCopies Text 1 TextBox Name txtStartPage Text 0 TextBox Name txtEndPage Text 0 Button Name btnPrint Export Label Text Export Type Label Text File Path TextBox Name txtExportFilePath Label Text File Name TextBox Name txtExportFileName ListBox Name lstExportType Button Text btnExport View CrystalReportViewer Anchor Top, Bottom, Left, Right ReportSource rdHowTo10_45. Type Excel and Word Document into the Items collection of lstExportType.6. Add the code in Listing 10.1 to the Click event of btnPrint. Listing 10.1 frmHowTo10_4.vb: Printing the Report Private Sub btnPrint_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnPrint.Click Dim po As PrintDialog Me.rdHowTo10_4.PrintToPrinter(Me.txtNumOfCopies.Text, False, _ Me.txtStartPage.Text, Me.txtEndPage.Text) End Sub7. Add the code in Listing 10.2 to the Click event of btnExport. This code tests the value of lstExportType and assigns the appropriate ExportFormatType and file extension (strExt). The DiskFileDestinationOptions information is supplied, and the Export method is called to complete the export. Listing 10.2 frmHowTo10_4.vb: Exporting the Report Private Sub btnExport_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnExport.Click Dim strExt As String Try With Me.rdHowTo10_4.ExportOptions Select Case lstExportType.SelectedItem Case Excel .ExportFormatType = _ CrystalDecisions ...