cancel
Showing results for 
Search instead for 
Did you mean: 

Exporting report based on a file (.rpt)

Former Member
0 Kudos

Hi

Not sure how to do this:

I have a winforms app with a Crystal Viewer on a Form. I load the ReportSource from a file

cryViewer.ReportSource = "C:\CryReport.rpt"

for instance.

What I want to do once the report is loaded and possibly refreshed by the user is to export the report to PDF/Excel/Word etc.

I have done this before with reports that are generated with a Crystal Report Class (reports embedded in the WinForm application) by setting up the ExportOptions and using the Export() function as is well documented everywhere.

I can't seem to work out how to Export if the Viewer has it's report originally brought in from a file.

Any ideas anyone?

Thanks in advance,

Julian

Accepted Solutions (1)

Accepted Solutions (1)

former_member183750
Active Contributor
0 Kudos

There are two ways of exporting a report;

1) Code

2) Using the export button on the viewer

Based on the above, I'm not sure what you are trying to do as you appear to be familiar with both options. If you are trying to view and export the report in one step, then you can not do this. You will have to run the report to the viewer and then rerun the report and export using code. E.g.; option (1) above then option (2).

Ludek

Follow us on Twitter http://twitter.com/SAPCRNetSup

Got Enhancement ideas? Try the [SAP Idea Place|https://ideas.sap.com/community/products_and_solutions/crystalreports]

Former Member
0 Kudos

Thanks for your prompt reply Ludek.

Based on a report that is pre-created in the (VB) WinForm app at design time, giving you a .vb Class file, I have exported a report with the following code before:

Public Function CrystalConvert(ByVal rptIn As CrystalDecisions.CrystalReports.Engine.ReportClass, _

ByVal inFormat As CrystalDecisions.Shared.ExportFormatType, _

ByVal strInPath As String) As Boolean

Try

Dim CrExportOptions As CrystalDecisions.Shared.ExportOptions

Dim CrDiskFileDestinationOptions As New CrystalDecisions.Shared.DiskFileDestinationOptions

'Set the destination path and file name

CrDiskFileDestinationOptions.DiskFileName = strInPath

'Set export options

CrExportOptions = rptIn.ExportOptions

With CrExportOptions

'Set the destination to a disk file

.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile

'Set the format

.ExportFormatType = inFormat

'Set the destination options to DiskFileDestinationOptions object

.DestinationOptions = CrDiskFileDestinationOptions

End With

'Export the report

rptIn.Export()

CrDiskFileDestinationOptions = Nothing

CrExportOptions = Nothing

Catch ex As Exception

ITAXError()

Return True

Finally

GC.Collect()

End Try

End Function

What I now need to do is do the same, but this time instead of the report being created inside the project at design time, I pass a file name to the Crystal Viewer, setting it's ReportSource and the viewer displaying the report. This means that I can't use the above function as the ReportSource is simply a file name string, whereas before I was passing in the class name that was auto-generated at design time.

What I need is for the user not to be able to click the Export button, but for them to click a button of my own design and do an export then so that I have control over where it is stored on the system, and they are shielded from any Explorer type interface.

the following code snippet is almost there:

Dim rpt As New ReportDocument

rpt.Load(CStr(cryViewer.ReportSource))

rpt.SaveAs(strTempFileNameCRY, True)

rpt.ExportToDisk(ExportFormatType.WordForWindows, strTempFileNameDoc)

The only problem is that the user may have clicked the Refresh button, and the data seen on screen may be different to what was originally generated from the .rpt, and the the above snippet is based on the .rpt.

Is there a way of exporting what we're seeing on screen without using the Crystal Viewer Export button?

Sorry for the lengthy reply, but if there is an answer, then I'd like to know it.

Many thanks for your time

Julian

former_member183750
Active Contributor
0 Kudos

What you could do, at the same time you are exporting to what ever format, export to an rpt file format. This will export a report with saved data. Then view this exported report (CrystalReportViewer1.ReportSource = <path to just exported rpt>. In this way. you are ensuring the same data that was exported is viewed in the viewer. If you do not want the user to refresh the report, simply hide the refresh button.

Not sure if I'm answering the question (sorry, call me dense), but I'll get somebody else here to look at this thread and comment if need be...

- Ludek

0 Kudos

Hi Julian,

In your old code you were loading the report through the Engine, in your new code you are simply using the Viewer.

What version of Cr and VS are you using now?

Thanks

Don

Former Member
0 Kudos

Hi Don and Ludek

In response to you both (thanks):

Don, we are using the version that came with Visual Studio 2005. On the verge of upgrading to the latest version in Visual Studio.

Ludek, the probelm I have is that I don't know how to initiate an Export from the Crystal Viewer without going through the ExportReport button or routine that brings up the Browser to choose a place and name to export to.

So you think something along the lines of

Dim rpt1 As New ReportDocument

rpt1.Load(CStr(cryViewer.ReportSource))

rpt1.SaveAs(strTmpCRFilename, False)

Dim rpt2 As New ReportDocument

rpt2.Load(strTmpCRFilename)

rpt2.ExportToDisk(ExportFormatType.WordForWindows, strTmpWDFilename)

Any help as always is greatly appreciated.

Thanks

Julian

former_member183750
Active Contributor
0 Kudos

There is no way to initaite the export via a viewer without hitting the export button. The viewer API is quite limited.

- Ludek

0 Kudos

Great,

Then you'll have the option to set what export types are allowed in the viewer.

It's C# but works the same in VB:

// set up the format export types:

int myFOpts = (int)(

CrystalDecisions.Shared.ViewerExportFormats.RptFormat |

CrystalDecisions.Shared.ViewerExportFormats.PdfFormat |

CrystalDecisions.Shared.ViewerExportFormats.RptrFormat |

CrystalDecisions.Shared.ViewerExportFormats.XLSXFormat |

CrystalDecisions.Shared.ViewerExportFormats.CsvFormat |

CrystalDecisions.Shared.ViewerExportFormats.EditableRtfFormat |

CrystalDecisions.Shared.ViewerExportFormats.ExcelRecordFormat |

CrystalDecisions.Shared.ViewerExportFormats.RtfFormat |

CrystalDecisions.Shared.ViewerExportFormats.WordFormat |

CrystalDecisions.Shared.ViewerExportFormats.XmlFormat |

CrystalDecisions.Shared.ViewerExportFormats.ExcelFormat |

CrystalDecisions.Shared.ViewerExportFormats.ExcelRecordFormat);

//CrystalDecisions.Shared.ViewerExportFormats.NoFormat); // does nothing except show no export types

// Ignore above and allow all types.

//int myFOpts = (int)(CrystalDecisions.Shared.ViewerExportFormats.AllFormats);

crystalReportViewer1.AllowedExportFormats = myFOpts;

You simply OR them all together or just the ones you want enabled then when the user hits the export button in the viewer that is the only options they get to choose from.

Thanks again

Don

Former Member
0 Kudos

Hi guys,

Thanks for your help.

'There is no way to initaite the export via a viewer without hitting the export button. The viewer API is quite limited.' is what I was after, so that I know if I'm doing a work around, I'm not missing something.

I've got something to go on, so I won't bother you with this question any longer.

Thanks again

Julian

0 Kudos

You could always create your own export button, hide CR Viewers button and handle the export dialog boxes yourself. That way you do have full control over what export types are allowed.

Which I assume is what you are going to do now...

PS - the code snippet is for CR for VS 2010. Earlier versions did not have this ability.

Thanks again

Don

Answers (0)