Crystal Report Viewer - 输入参数后导出/打开PDF

时间:2010-06-30 18:23:02

标签: c# asp.net crystal-reports

我有一个报告查看器,我希望在用户输入参数并单击提交按钮(而不是在报告查看器窗口中打开报告)后,以PDF格式打开相关报告。 / p>

我在其他地方做了一些阅读并发现了这个片段

rptSP.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, "Report");

我还发现了推荐以下内容的帖子:

  

编写代码以将报告导出到CrystalReportViewer_BeforeRenderObject事件内的pdf。

但是我过去没有做太多。任何人都可以提供代码示例,或链接到显示此解决方案的示例吗?

3 个答案:

答案 0 :(得分:1)

您可以在提交按钮的点击事件中获取报告参数,并根据这些参数获取报告数据,然后可以将其导出。

以下是执行此操作的示例代码。

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);

答案 1 :(得分:1)

DataTable _table = RetObj.EgpReceipt_PrintFC(DummId);

rptDoc.Load(Server.MapPath("EgpReceiptPrintFC.rpt"));
rptDoc.SetDataSource(_table);

rptDoc.ExportToHttpResponse(ExportFormatType.PortableDocFormat, HttpContext.Current.Response, false, "Redeemed");

答案 2 :(得分:0)

我挖掘了我在2006年提交的旧代码项目文章 - 它从Crystal Reports项目导出为pdf。我不确定这些库是否相关 - 更糟糕的是,它在VB中。希望这有效,或者至少指向正确的方向。

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***************************

然后是方法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