Crystal Report Viewer - Export to/open a PDF after entering parameters
I have a report viewer in place, and i would like the report in question to be opened in a PDF after the user enters parameters and clicks the submit button (instead of the report being opened in the report viewer window.)
I did some reading elsewhere and found this snippet
rptSP.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, "Report");
and i also found a posting where the following was recommended:
Write code 开发者_Python百科to export the report to pdf inside of the CrystalReportViewer_BeforeRenderObject event.
but I haven't made it much past that. Can anyone provide a code example, or link to one which shows this solution in place?
You can fetch report parameters on click event of submit button, and fetch report data based on these parameters, and after that, you can export it.
Here is a sample code for doing this.
ReportDocument rptDoc = new ReportDocument();
rptDoc.Load(Server.MapPath("ETR0040.rpt"));
// Fetch report parameters to retrieve report data.
...
// Retrieve report data.
DataSet dsResult = DBGateway.ExecuteCommand('command');
rptDoc.SetDataSource(dsResult);
// Report parameters - to be passed if there are any parameters which should be passed to report.
rptDoc.SetParameterValue("CustomerCode", customerCode);
DiskFileDestinationOptions diskOpts = new DiskFileDestinationOptions();
string targetFileName = Request.PhysicalApplicationPath + "Reports\\TempReports\\" + (new Random()).Next() + ".pdf";
rptDoc.ExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
rptDoc.ExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
diskOpts.DiskFileName = targetFileName;
rptDoc.ExportOptions.DestinationOptions = diskOpts;
// Export report ... Server-Side.
rptDoc.Export();
FileInfo file = new FileInfo(targetFileName);
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/pdf";
Response.TransmitFile(file.FullName);
DataTable _table = RetObj.EgpReceipt_PrintFC(DummId);
rptDoc.Load(Server.MapPath("EgpReceiptPrintFC.rpt"));
rptDoc.SetDataSource(_table);
rptDoc.ExportToHttpResponse(ExportFormatType.PortableDocFormat, HttpContext.Current.Response, false, "Redeemed");
I dug through an old codeproject article that I submitted back in 2006 - which exports to pdf from a Crystal Reports project. I'm not sure if the libraries are relevant anymore - and even worse, it's in VB. Hope this works, or at least points in the right direction.
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
'
' <snip>
Dim ExportToFile As New CsReports.Reports
ExportFile = ExportPath + Session.SessionID.ToString + ".pdf"
ExportToFile.ReportToPDF(crReportDocument, ExportFile)
Try
' Export the report
crReportDocument.Export()
Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "application/pdf"
Response.WriteFile(ExportFile)
Response.Flush()
Response.Close()
System.IO.File.Delete(ExportFile)
Catch err As Exception
Throw err 'MessBox("Error: Export Failed.!" + err.Message.ToString)
End Try 'Adobe Acrobat Export***************************
Then there's the method ReportToPDF:
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports System
Imports System.Data
Imports System.Web.UI.Page
Imports System.IO
Namespace CsReports
Public Class Reports
Inherits System.Web.UI.Page
Dim crExportOptions As ExportOptions
Dim crDiskFileDestinationOptions As DiskFileDestinationOptions
Public Sub ReportToPDF(ByVal crReportName As ReportDocument, ByVal ExportFile As String)
' Create a new instance of the diskfiledestinationoptions class and
' set variable called crExportOptions to the exportoptions class of the reportdocument.
crDiskFileDestinationOptions = New DiskFileDestinationOptions()
crExportOptions = crReportName.ExportOptions
'Set this file as the filename property for the DestinationOptions class
crDiskFileDestinationOptions.DiskFileName = ExportFile
'set the required report ExportOptions properties
With crExportOptions
.DestinationOptions = crDiskFileDestinationOptions
.ExportDestinationType = ExportDestinationType.DiskFile
.ExportFormatType = ExportFormatType.PortableDocFormat
End With
End Sub
End Class
End Namespace
精彩评论