Also in this example - how to export cr to PDF
1. Create Window Application
I will display in my report records from Department Table
2. Add DataSet to it
3. Create new table in it with 2 column names
DepartmentName and DepartmentPicture
DataSet will look like this
4. Add CrystalReport
5. Add DataSet to it
6. Double click Form1 and add the following
Imports System.Data.OleDb
Imports System.IO
Imports CrystalDecisions.Shared
7. Add the following code to page load
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim dsDataSet As New DataSet1
Dim conConnection As New OleDbConnection
conConnection.ConnectionString = "Provider = SqlOleDb;Data Source=DANIEL-MOBILE2\DANIELTESTING;Initial Catalog=School;Persist Security Info=True;User ID=sa;Password=VerySecret"
Dim strSql As String = "SELECT DepartmentName, DepartmentImage FROM Dpt WHERE (DepartmentId = 93)"
' DepartmentId 94 has different picture --look at the data above
' Dim strSql As String = "SELECT DepartmentName, DepartmentImage FROM Dpt WHERE (DepartmentId = 94)"
Dim daDataAdapter As New OleDbDataAdapter(strSql, conConnection)
daDataAdapter.Fill(dsDataSet, "DPT")
Dim crDoc As New CrystalReport1
Dim crExportOptions As ExportOptions
Dim crDiskFileDestinationOptions As New DiskFileDestinationOptions
Dim crFormatTypeOptions As New PdfRtfWordFormatOptions
Dim strFolder As String = "C:\TEMP\Test\"
If Not Directory.Exists(strFolder) Then
Directory.CreateDirectory(strFolder)
End If
crDoc.SetDataSource(dsDataSet.Tables("DPT"))
crDiskFileDestinationOptions.DiskFileName = strFolder & "MyPDF.pdf"
crExportOptions = crDoc.ExportOptions
With crExportOptions
.ExportDestinationType = ExportDestinationType.DiskFile
.ExportFormatType = ExportFormatType.PortableDocFormat
.DestinationOptions = crDiskFileDestinationOptions
.FormatOptions = crFormatTypeOptions
End With
crDoc.Export()
End Sub
8. add picture to your report
9. Right click on it, select FormatObject
10. Click Picture Tab then Graphic Location
11. Select from your dataset field that has picture location path
12. In order to compile this project you will also have to follow these steps
Right click project and select Properties
Click compile and advanced compile options
Change target framework to .NET Framework 4
13. Also modify App.config to
<configuration>
<startup useLegacyV2RuntimeActivationPolicy ="true">
<supportedRuntime version="v4.0" />
</startup>
</configuration>
Now just ensure you have your pictures in test folder, run and depending of department id - proper image will be displayed in your report. Also report will be exported as PDF to the same test folder.
No comments:
Post a Comment