Monday, May 11, 2009

Exporting Crystal Report to Excel or PDF format

Try

Cursor = Cursors.WaitCursor

Try

If Not System.IO.Directory.Exists("C:\report") Then

System.IO.Directory.CreateDirectory(

"C:\report")

End If

Catch ex As Exception

MsgBox(

"You have not sufficient rights to create folder. Please contact your Administrator.")

Exit Sub

End Try

Dim strExportFile As String = "C:\report\Report.xls"

rs.ExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile

If strExportFile.ToUpper.EndsWith(".XLS") Then

rs.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.Excel

ElseIf strExportFile.ToUpper.EndsWith(".PDF") Then

rs.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat

End If

If File.Exists(strExportFile) Then

strExportFile =

"C:\report\Report_" & Date.Now.Day & Date.Now.Month & Date.Now.Year & "_" & Date.Now.Hour & Date.Now.Minute & Date.Now.Second & ".xls"

End If
 
Application.DoEvents()

rs.ExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile

rs.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.Excel

Dim objOptions As New CrystalDecisions.Shared.DiskFileDestinationOptions

objOptions.DiskFileName = strExportFile

rs.ExportOptions.DestinationOptions = objOptions

rs.Export()

Application.DoEvents()

objOptions =

Nothing

Cursor = Cursors.Default

MsgBox(

"Export Completed" & vbCrLf & vbCrLf & "Report Generated At : " & strExportFile)

System.Diagnostics.Process.Start(strExportFile)

Catch ex As Exception

Cursor = Cursors.Default

MsgBox(

"Exception in Exporting Report " & ex.ToString)

Exit Sub

End Try
      

No comments:

Post a Comment