VB.NET Crystal Reports Export to Excel

To export data from Crystal Reports to the Excel format, we employ the Crystal Reports CrExportOptions. Furthermore, it is essential to configure the ExcelFormatOptions and set the ExportFormatType to Excel. In the subsequent example, you will find a demonstration of how to export a Crystal Reports document as an Excel file.

It is important to note that all the programming samples presented in these tutorials for Crystal Reports are built upon the crystaldb database. Prior to commencing this tutorial, I highly recommend acquainting yourself with the Database Structure. To access the database structure, kindly click on the following link: Click here to view the Database Structure.

In this tutorial, we will be utilizing our previous program, which thoroughly explains the step by step Crystal Report process of pulling data from a database into Crystal Reports. To ensure a comprehensive understanding, I suggest reviewing the step-by-step guide titled "Crystal Report in VB.NET" before proceeding with this section.

In the aforementioned step-by-step Crystal Report guide, we extract all the data from the Product table. However, in the current scenario, our objective is to export this retrieved data to an Excel file format.

Select the default form (Form1.vb) you created in VB.NET and drag two buttons (Button1, Button2 ) and CrystalReportViewer control to your form.

Select Form's source code view and import the following :

Imports CrystalDecisions.CrystalReports.Engine

Put the following source code in the button click events

Full Source VB.NET
Imports CrystalDecisions.CrystalReports.Engine Imports CrystalDecisions.Shared Public Class Form1 Dim cryRpt As New ReportDocument Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt") CrystalReportViewer1.ReportSource = cryRpt CrystalReportViewer1.Refresh() End Sub Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click Try Dim CrExportOptions As ExportOptions Dim CrDiskFileDestinationOptions As New _ DiskFileDestinationOptions() Dim CrFormatTypeOptions As New ExcelFormatOptions CrDiskFileDestinationOptions.DiskFileName = _ "c:\crystalExport.xls" CrExportOptions = cryRpt.ExportOptions With CrExportOptions .ExportDestinationType = ExportDestinationType.DiskFile .ExportFormatType = ExportFormatType.Excel .DestinationOptions = CrDiskFileDestinationOptions .FormatOptions = CrFormatTypeOptions End With cryRpt.Export() Catch ex As Exception MsgBox(ex.ToString) End Try End Sub End Class

cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt")

The Crystal Report is in your project location, there you can see CrystalReport1.rpt . So give the full path name of report here.

When you run this program you will get the Excel file (crystalExport.xls) in your computer's C: