Sunteți pe pagina 1din 8

Crystal Reports for Visual Studio .

NET
Export and Print a Crystal Report

Overview
This white paper will walk you through creating a .NET Windows
application using Crystal Reports for Visual Studio .NET.

In the first section, you will create a basic Visual Basic .NET Windows
application that loads a report and previews it in the
CrystalReportViewer control. The next section explains how to export
the report to a portable document file (PDF) through code. The final
section explains how to print the application’s report to a printer through
code.

Contents
INTRODUCTION ............................................................................................ 2
CREATE A SIMPLE APPLICATION .................................................................. 2
Add a Crystal Report to the Project.............................................................3
Run the Project ............................................................................................4
EXPORT THE REPORT TO PDF ..................................................................... 5
Add an Export Button .................................................................................5
Add Exporting Code ....................................................................................5
Run the Project ............................................................................................6
PRINT THE REPORT TO A PRINTER ............................................................... 6
Add a Print Button......................................................................................6
Add Printing Code.......................................................................................7
Run the Project ............................................................................................7
FINDING MORE INFORMATION ...................................................................... 8

1/27/2005 1:54 PM Copyright © 2005 Business Objects. All rights reserved. Page 1
CR for VS .NET Export and Print a Report

Introduction
Crystal Reports for Visual Studio .NET extends the powerful reporting
capability of Crystal Reports on the Microsoft .NET platform. You can
use the Crystal Report Designer in Visual Studio .NET to create a new
Crystal Report or modify an existing Crystal Report. You can then keep
the report on a local computer, as is the case with this example, or
publish it as a Report Web Service on a web server. You can host the
report with the CrystalReportViewer control.

Create a Simple Application


1. Open the Visual Studio .NET IDE.
2. On the File menu, click New then click Project.
3. The New Project dialog box (Figure 1) opens. In the Project Types
pane, click Visual Basic Projects and in the Template pane click
Windows Application. At the bottom of the dialog box, in the Name
field type, “ExportAndPrintReport” and keep the default path for
the Location field. Click the OK button to create the project.

Figure 1

The default path for the location of the project is:


NOTE C:\Documents and Settings\<user>\My Documents\Visual Studio Projects
where <user> is the name of the user currently logged into the machine.

1/27/2005 1:54 PM Copyright © 2005 Business Objects. All rights reserved. Page 2

crnet_exportandprintreport.pdf
CR for VS .NET Export and Print a Report

Add a Crystal Report to the Project


Now that you have a blank project started, continue to build a sample
application that uses Crystal Reports for Visual Studio .NET.

1. In the Toolbox, double-click CrystalReportViewer to insert it into


the form.
2. In the Properties window (Figure 2), click Dock. Set this property to
Fill. This allows the viewer to resize with the form.

Figure 2

3. On the Project menu, click Add Existing Item.


4. In the Add Existing Item dialog box (Figure 3), browse to:
C:\Program Files\Microsoft Visual Studio .NET\Crystal Reports\Samples\Reports\
General Business\

5. Change the Files of Type: drop-down box to All Files (*.*), click
World Sales Report.rpt then click the Open button.

Figure 3

6. In the Solution Explorer, double-click Form1.vb to display the form.


Right-click the form then click View Code.
7. Click on the ‘+’ sign next to “Windows Form Designer generated code”
to expand the rest of the code.

1/27/2005 1:54 PM Copyright © 2005 Business Objects. All rights reserved. Page 3

crnet_exportandprintreport.pdf
CR for VS .NET Export and Print a Report

8. At the top of the code view, type the following in code:


Imports CrystalDecisions.CrystalReports.Engine

9. Above the existing code:


#Region " Windows Form Designer generated code "

add the following line of code:

Dim crReportDocument As World_Sales_Report

10. After “InitializeComponent()” in the form’s constructor, type the


following code:
CrReportDocument = New World_Sales_Report()
CrystalReportViewer1.ReportSource = crReportDocument

Run the Project


1. On the Build menu, click Build Solution. This will compile the
project and build the assembly, which in this project, is an .EXE file.
The Output window shows the results of the building process.

If the compiler found any errors/warnings in the project, a task is created for each item,
NOTE and the Task List window will list all the tasks (errors/warnings).

2. On the Debug menu, click Start Without Debugging. This loads the
project and runs it without loading any of the debugging features.
The application runs faster if the debugging controls are not loaded.
When the application is run, you will see the World Sales Report in the
The CrystalReportViewer
control used to be called the CrystalReportViewer control (Figure 4).
Windows Form Viewer.

Figure 4

1/27/2005 1:54 PM Copyright © 2005 Business Objects. All rights reserved. Page 4

crnet_exportandprintreport.pdf
CR for VS .NET Export and Print a Report

Export the Report to PDF


To export the report to a portable document file (PDF), take the
application, add a button to the form and add code. You can either start
a new application or build on the existing application from the previous
section. The following steps assume you are building on the existing
application.

Add an Export Button


1. In the Visual Studio Solution Explorer, right-click Form1.vb then click
View Designer.
2. In the Toolbox, click Button then drag-and-drop this button onto the
form.
3. In the ‘Properties’ window for this button change the ‘Text’ property
to ‘Export to PDF’ (you may need to widen the button to see all this
text). Change the ‘Name’ property to ‘ExportToPDF’.

Figure 5

To avoid confusion you may want to disable the export button on the
NOTE CrystalReportViewer. To do this, in the Properties window for the CrystalReportViewer,
change the ShowExportButton property to False.

Add Exporting Code


1. Add the following code at the top of the code page (non-italicized
code already exists, add italicized code only):
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared

2. Above the line of code:


#Region " Windows Form Designer generated code "

Add the following code:

Dim crReportDocument As World_Sales_Report


Dim crExportOptions As ExportOptions
Dim crDiskFileDestinationOptions As DiskFileDestinationOptions

1/27/2005 1:54 PM Copyright © 2005 Business Objects. All rights reserved. Page 5

crnet_exportandprintreport.pdf
CR for VS .NET Export and Print a Report

3. Double-click the button on the form. This will create an


ExportToPDF_Click event in the code. Add the following code:
CrDiskFileDestinationOptions = New DiskFileDestinationOptions()
CrDiskFileDestinationOptions.DiskFileName = “C:\exported.pdf”
CrExportOptions = crReportDocument.ExportOptions
With crExportOptions
.DestinationOptions = crDiskFileDestinationOptions
.ExportDestinationType = ExportDestinationType.DiskFile
.ExportFormatType = ExportFormatType.PortableDocFormat
End With
CrReportDocument.Export()

MessageBox.Show (“Report Exported”)

As you enter the ExportFormatType you will see a drop-down list of all the possible export
formats. Try exporting to these other formats as well. Remember to change the extension
NOTE of the DiskFileName value to the appropriate extension. For instance is you choose the
Excel format type, change the DiskFileName value to “C:\exported.xls”.

Run the Project


1. On the Build menu, click Build Solution.
2. On the Debug menu, click Start Without Debugging.
3. When the report appears, click the Export to PDF button. A message
box will appear saying, “Report Exported” and the PDF file will be saved
to the root of your C drive.

Print the Report to a Printer


To print the application’s report to a printer through code you need to
insert another button onto the form then add code to that button’s click
event.

Add a Print Button


1. In the Visual Studio Solution Explorer, right-click Form1.vb then click
View Designer.
2. In the Toolbox, click Button and drag-and-drop this button onto the
form.
3. In the Properties window for this button change the Text property to
‘Print’ and change the Name property to ‘Print’.

1/27/2005 1:54 PM Copyright © 2005 Business Objects. All rights reserved. Page 6

crnet_exportandprintreport.pdf
CR for VS .NET Export and Print a Report

Figure 6

To avoid confusion you may want to disable the print button on the CrystalReportViewer.
NOTE To do this, in the Properties window for the CrystalReportViewer, change the
ShowPrintButton property to False.

Add Printing Code


1. Double-click the Print button you added in the previous steps. This
creates a Print_Click event in the code.
2. Add the following code to the Print_Click event:
CrReportDocument.PrintOptions.PrinterName = “<PrinterName>”
The arguments for the
PrintToPrinter method are: CrReportDocument.PrintToPrinter(1, True, 1, 1)
number of copies, collated, start
page and end page respectively.

Run the Project


1. On the Build menu, click Build Solution.
2. On the Debug menu, click Start Without Debugging.
3. When the report appears, click the Print button and the report will
print to the printer specified in the PrinterName property.

1/27/2005 1:54 PM Copyright © 2005 Business Objects. All rights reserved. Page 7

crnet_exportandprintreport.pdf
CR for VS .NET Export and Print a Report

Finding More Information


For more information and resources, refer to the product documentation
and visit the support area of the web site at: www.businessobjects.com

You can also learn how to create a .NET Windows application using
Crystal Reports for Visual Studio .NET by following our sample
applications. Search for the files:

vbnet_win_samples.exe

and

vbnet_web_samples.exe

at:

http://support.businessobjects.com/search

www.businessobjects.com

Business Objects owns the following U.S. patents, which may cover products that are offered and sold by Business
Objects: 5,555,403, 6,247,008 B1, 6,578,027 B2, 6,490,593 and 6,289,352. Business Objects, the Business Objects
logo, Crystal Reports, and Crystal Enterprise are trademarks or registered trademarks of Business Objects SA or its
affiliated companies in the United States and other countries. All other names mentioned herein may be trademarks
of their respective owners. Product specifications and program conditions are subject to change without notice.
Copyright © 2005 Business Objects. All rights reserved.

1/27/2005 1:54 PM Copyright © 2005 Business Objects. All rights reserved. Page 8

crnet_exportandprintreport.pdf

S-ar putea să vă placă și