Forum Moderators: open

Message Too Old, No Replies

Crystal report problems

         

davelye

5:21 am on Dec 15, 2004 (gmt 0)

10+ Year Member



I am currently doing a project using Crystal Report(CR) 9 and visual basic.net window forms.
Do anyone here know how to export the report to (excel,doc...) progrmmatically without using the 'export report' button in the CRViewer. Or is there any website to teach the method?

I tried using the code in a book but can't achieve what the book stated. below is part of the code. Following the book, I drag the reportdocument icon from the toolbox to my window form and and name it rdViewer. But when i run the program no compile error but the program go from 'Try' to 'MessageBox.Show(excp.Message)'. in other word always go into exception. What went wrong here? i tried to troubleshoot and realize that the book has a step that 'Choose a ReportDocument' dialog box that will link to the report whereby i don't know how to set. in addition i found that the soft code they provide the ReportSource in the CRViewer is also different from mine. Their is 'rdHowTo10_4 [VB.Net___Chapter_10.rptHowTo10_1]' rdHowTo10_4 is the reportdocument they created and rptHowTo10_1 is the Crystal report that is in the solution explorer.

Private Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExport.Click
Dim strExt As String

Try
With Me.rdViewer.ExportOptions
Select Case lstExportType.SelectedItem

Case "Excel"
.ExportFormatType = _
CrystalDecisions.[Shared].ExportFormatType.Excel
strExt = ".xls"

Case "Word Document"
.ExportFormatType = _
CrystalDecisions.[Shared].ExportFormatType.WordForWindows
strExt = ".Doc"

End Select

.ExportDestinationType = _
CrystalDecisions.[Shared].ExportDestinationType.DiskFile

Dim ddo As New CrystalDecisions.Shared.DiskFileDestinationOptions()

ddo.DiskFileName = "c:\" & "Test" & strExt

.DestinationOptions = ddo

End With

Me.rdViewer.Export()

MessageBox.Show("Report Exported!")

Catch excp As Exception
MessageBox.Show(excp.Message)

End Try
End Sub

tomasz

9:19 pm on Dec 17, 2004 (gmt 0)

10+ Year Member



example of WebService for SQL source can be modify for you use

Dim oRpt As New ReportDocument

<WebMethod()> Public Function ExportReport(ByVal ReportFileName As String, _
ByVal ExportFileName As String, _
ByVal ReportTitle As String, _
ByVal ReportFormula As String, _
ByVal ReportLogon As String) As String
Dim sPath As String = Context.Request.PhysicalApplicationPath '"d:\inetpub\wwwroot\test"

Try

Dim arrLogonInfo As String() = Split(ReportLogon, ";")
'Loads report from repository location
oRpt.Load(sPath + "reports\" + ReportFileName)

'single table logon ; delimited string = Server;Database;UserID;Password
Dim exportOpts As New ExportOptions
Dim diskOpts As New DiskFileDestinationOptions

Dim crLogonInfo As CrystalDecisions.Shared.TableLogOnInfo
crLogonInfo = oRpt.Database.Tables(0).LogOnInfo
crLogonInfo.ConnectionInfo.ServerName = arrLogonInfo(0)
crLogonInfo.ConnectionInfo.DatabaseName = arrLogonInfo(1)
crLogonInfo.ConnectionInfo.UserID = arrLogonInfo(2)
crLogonInfo.ConnectionInfo.Password = arrLogonInfo(3)

oRpt.Database.Tables(0).ApplyLogOnInfo(crLogonInfo)

exportOpts = oRpt.ExportOptions

exportOpts.ExportDestinationType = ExportDestinationType.DiskFile
diskOpts.DiskFileName = sPath + "temp\" + ExportFileName
exportOpts.DestinationOptions = diskOpts
Dim sExt As String = UCase(Right(ExportFileName, 3))

Select Case sExt
Case Is = "PDF"
exportOpts.ExportFormatType = ExportFormatType.PortableDocFormat
Case Is = "DOC"
exportOpts.ExportFormatType = ExportFormatType.WordForWindows
Case Is = "XLS"
exportOpts.ExportFormatType = ExportFormatType.Excel
Case Else
exportOpts.ExportFormatType = ExportFormatType.PortableDocFormat
End Select

Dim ReportFields As FormulaFieldDefinitions = oRpt.DataDefinition.FormulaFields()

oRpt.DataDefinition.FormulaFields("ReportTitle").Text = ReportTitle '"'" & Replace(ReportTitle, vbCrLf, "' + chr(13) + chr(10) + '") & "'" 'ReportTitle
Dim sExistingFormula As String = oRpt.RecordSelectionFormula
'check for existing formula

If InStr(sExistingFormula, "{") > 0 And ReportFormula <> "" Then
oRpt.RecordSelectionFormula = oRpt.RecordSelectionFormula + " and " + ReportFormula
Else
oRpt.RecordSelectionFormula = ReportFormula
End If

oRpt.Export()
ExportReport = "OK"
Catch e As Exception
Dim sMes As String = "Error: " + e.Message
ExportReport = sMes
End Try
oRpt.Close()
oRpt = Nothing
End Function

davelye

3:55 am on Dec 23, 2004 (gmt 0)

10+ Year Member



Thank for your code. I read it and tried to modify together with the help of code from the book and web. I came out with the code below. I run the program and when I click the export button I encountered the error message “Missing parameter field current value”. To be specific is the line code “crReportDocument.Export()”
Basically there are 2 parts in this code. The first part is to set the parameters in the crystal report that I have a created (here KKSSreport.rpt). In the crystal report I had set 2 parameters @PatID and @PatDate and a selection formula “{KKSSScore.PatientID} = {?PatID} and {KKSSScore.KKSSDate} = {?PatDate}”. I can select the required PatID and PatDate that I want. Example PatID= 1 and visit on 01/10/2004. the report will retrieve data for this ID and date. From “Dim exportFileName As String = "exportedReportK.rpt" ” line to the end is suppose to do an export. If I use the export code without the parameters part on another crystal report that do not have parameters and selection formula. It works. exportedReportK.doc is saved as a word document in the bin folder of my program folder
I been searching for the answer from friends, books and the web, found some but still can’t solve the problem. Some solution they offer are i must set the parameters for the document when i try to export it” I tried to search for the answer but still can’t. [The url : [groups.yahoo.com...]
Found a new url and I will read it now. [dotnet247.com...]
Can you tell me how to correct this error? Thank you
Regards dave

Private Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExport.Click
MyCrystalReportViewer1.ReportSource = Nothing
Dim PatID As New ParameterDiscreteValue()
Dim PatDate As New ParameterDiscreteValue()
PatID.Value = strPatID
PatDate.Value = strPatDate
Dim paramList As New ParameterFields()
Dim paramTemp As New ParameterField()
paramTemp = New ParameterField()
paramTemp.ParameterFieldName = "@PatID"
paramTemp.CurrentValues.Add(PatID)
paramList.Add(paramTemp)
paramTemp = New ParameterField()
paramTemp.ParameterFieldName = "@PatDate"
paramTemp.CurrentValues.Add(PatDate)
paramList.Add(paramTemp)
MyCrystalReportViewer1.ParameterFieldInfo = paramList

MyCrystalReportViewer1.ReportSource = Application.StartupPath & "\..\rptTestA.rpt"

Dim exportFileName As String = "exportedReportK.rpt" Dim exportPath As String = Application.StartupPath & "\" & exportFileName
Dim crReportDocument As New ReportDocument()
crReportDocument.Load("C:\Documents and Settings\kimcjw\Desktop\SleepMonitorSystem3CRDone\KKSSreport.doc")
Dim crExportOptions As ExportOptions
Dim crDestOptions As New DiskFileDestinationOptions()
crDestOptions.DiskFileName = exportPath
crExportOptions = crReportDocument.ExportOptions
crExportOptions.DestinationOptions = crDestOptions
crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile
crExportOptions.ExportFormatType = ExportFormatType. WordForWindows
crReportDocument.Export()

End Sub

tomasz

2:05 pm on Dec 23, 2004 (gmt 0)

10+ Year Member



davelye,
Well the reason I am using formulas versus parameters that you may have different numbers of parameters per report but only one formula, therefore it is easier to create "template" and use one generic code.
I do not know the answer to your problem but you can check with asp.net forums they have several good posters there.
good luck

[asp.net...]

I could be wrong but you should not be using viewer see if this will work ..

Private Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExport.Click
MyCrystalReportViewer1.ReportSource = Nothing

Dim exportFileName As String = "exportedReportK.rpt" Dim exportPath As String = Application.StartupPath & "\" & exportFileName
Dim crReportDocument As New ReportDocument()
crReportDocument.Load("C:\Documents and Settings\kimcjw\Desktop\SleepMonitorSystem3CRDone\KKSSreport.doc")

Dim PatID As New ParameterDiscreteValue()
Dim PatDate As New ParameterDiscreteValue()
PatID.Value = strPatID
PatDate.Value = strPatDate
Dim paramList As New ParameterFields()
Dim paramTemp As New ParameterField()
paramTemp = New ParameterField()
paramTemp.ParameterFieldName = "@PatID"
paramTemp.CurrentValues.Add(PatID)
paramList.Add(paramTemp)
paramTemp = New ParameterField()
paramTemp.ParameterFieldName = "@PatDate"
paramTemp.CurrentValues.Add(PatDate)
paramList.Add(paramTemp)
crReportDocument.ParameterFieldInfo = paramList
'MyCrystalReportViewer1.ReportSource = Application.StartupPath & "\..\rptTestA.rpt"

Dim crExportOptions As ExportOptions
Dim crDestOptions As New DiskFileDestinationOptions()
crDestOptions.DiskFileName = exportPath
crExportOptions = crReportDocument.ExportOptions
crExportOptions.DestinationOptions = crDestOptions
crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile
crExportOptions.ExportFormatType = ExportFormatType. WordForWindows
crReportDocument.Export()