Vb.net – Crystal Report exporting a report as excel file

crystal-reportsvb.net

I am creating a crystal report in VB 2008 which generate a MS Access file in the Crystal Report Viewer, and i want to export my work in crystal report viewer as excel file. Can this be possible???

Best Answer

I agree with @Andrew about the MS Access file, that doesn't quite make sense. Here is some sample code that shows how to export a report as an .xls file. The code is (obviously) assigned to a button.

Private Sub ButtonExport_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

You do know that the CrystalReportViewer has a toolbar with an export button, which can export to Excel without the need of any extra code. You can enable/disable that button in the CrystalReportViewer properties or using code like:

<CR:CrystalReportViewer  ....    HasExportButton="true" ... />
Related Topic