Saturday, April 11, 2009

How to Convert a Crystal Report to Word

This Code shows how to convert a Crystal Report to Word Document.


'First Create a crystal Report.
' Drag and drop the Crystal Report Viewer in the form
' copy this code on top of the code
Imports System.Data
Imports System.Data.SqlClient
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared

Private myDS As New Dataset1() ' Dataset you created.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim rpt As New CrystalReport1() 'The report you created.
Dim myConnection As SqlConnection
Dim MyCommand As New SqlCommand()
Dim myDA As New SqlDataAdapter()

Try

myConnection = New SqlConnection("Data Source=localhost;Integrated Security=SSPI;" & _
"Initial Catalog=pubs;")
MyCommand.Connection = myConnection
MyCommand.CommandText = "SELECT * FROM authors"
MyCommand.CommandType = CommandType.Text
myDA.SelectCommand = MyCommand

myDA.Fill(myDS, "authors")
rpt.SetDataSource(myDS)
CrystalReportViewer1.ReportSource = rpt

Catch Excep As Exception
MessageBox.Show(Excep.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'This Code is used to Convert to word document
Dim reportWord As New CrystalReport1() ' Report Name
Dim strExportFile As String = "d:\TestWord.doc"
reportWord.ExportOptions.ExportDestinationType = ExportDestinationType.DiskFile
reportWord.ExportOptions.ExportFormatType = ExportFormatType.WordForWindows
Dim objOptions As DiskFileDestinationOptions = New DiskFileDestinationOptions()
objOptions.DiskFileName = strExportFile
reportWord.ExportOptions.DestinationOptions = objOptions
reportWord.SetDataSource(myDS)
reportWord.Export()
objOptions = Nothing
reportWord = Nothing
End Sub

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home