0% found this document useful (0 votes)
8 views2 pages

Crystal Reprot Code Snippets

The document outlines a Visual Basic application that connects to a text database using OLEDB, retrieves data from a CSV file, and displays it in a DataGridView. It also includes functionality to export a Crystal Report to Excel and print the report using a CrystalReportViewer. The code demonstrates how to set up connections, handle exports, and manage report printing within the application.

Uploaded by

b.metzeller
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Crystal Reprot Code Snippets

The document outlines a Visual Basic application that connects to a text database using OLEDB, retrieves data from a CSV file, and displays it in a DataGridView. It also includes functionality to export a Crystal Report to Excel and print the report using a CrystalReportViewer. The code demonstrates how to set up connections, handle exports, and manage report printing within the application.

Uploaded by

b.metzeller
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Dim ConnectionString As String

ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _


"Data Source=" & "C:\Documents and Settings\...\My Documents\My
Database\Text" & ";" & _
"Extended Properties=""Text;HDR=YES;"""

Dim TextConnection As New


System.Data.OleDb.OleDbConnection(ConnectionString)
TextConnection.Open()

Dim SQLString As String = "SELECT * FROM TextFile.csv"

Dim da As New System.Data.OleDb.OleDbDataAdapter(SQLString, TextConnection)

Dim dt As New DataTable


da.Fill(dt)

DataGridView1.DataSource = dt

TextConnection.Close()

******************
export to excel
************************

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
************************
Print Report
***********************
Sub printReport()
' ADD REFERENCES.
' CrystalDecisions.CrystalReports.Engine
' CrystalDecisions.ReportSource

Dim Report As CrystalDecisions.CrystalReports.Engine.ReportDocument = New


CrystalDecisions.CrystalReports.Engine.ReportDocument
Dim CrystalReportViewer As CrystalDecisions.Web.CrystalReportViewer = New
CrystalDecisions.Web.CrystalReportViewer

CrystalReportViewer.BorderStyle = BorderStyle.Solid '


OPTIONAL
CrystalReportViewer.DisplayGroupTree = False '
OPTIONAL
CrystalReportViewer.Zoom(150) '
OPTIONAL
CrystalReportViewer.HasCrystalLogo = False

CrystalReportViewer.BestFitPage = False ' SET THIS FALSE, SO


YOU CAN SET THE WIDTH AND HEIGHT.
CrystalReportViewer.Width = "1200" : CrystalReportViewer.Height = "800"

' NOW LOAD THE REPORT.


Report.Load(System.AppDomain.CurrentDomain.BaseDirectory() & "\reports\" &
YourCrystalFileName & "")

Dim dt As New DataTable ' THE DATATABLE HAS THE DATA


FROM THE DATABASE.
Report.SetDataSource(dt) ' SET REPORT DATA SOURCE.
Report.PrintToPrinter(1, True, 0, 0) ' FINALY, PRINT IT.

CrystalReportViewer.ReportSource = Report
panReport.Controls.Add(CrystalReportViewer) ' ADD THE VIEWER WITH A
PANEL CONTROL ON YOUR WEB PAGE.
End Sub

End Sub
End Class

You might also like