如何在php中将参数传递给crystal report?

时间:2017-04-20 14:28:00

标签: php crystal-reports

我用php COM生成了水晶报告。现在我需要在没有提示的情况下将一些参数传递给报告。以下是代码:

<?php

//------  Variables ------
$my_report = "C:\\Apache2\htdocs\\test\\MyReport.rpt";  //This must be the full path to the file
$my_pdf = "C:\\Apache2\htdocs\\test\\MyReport.pdf";

//------ Create a new COM Object of Crytal Reports 10 ------
$ObjectFactory= new COM("CrystalReports10.ObjectFactory.1");

//------ Create a instance of library Application -------
$crapp = $ObjectFactory->CreateObject("CrystalDesignRunTime.Application.10");

//------ Open your rpt file ------
$creport = $crapp->OpenReport($my_report, 1);

//------ Set database logon info ------
$creport->Database->Tables(1)->SetLogOnInfo("MYSERVER", "Database", "user", "password");

//------ Suppress the Parameter field prompt or else report will hang ------
$creport->EnableParameterPrompting = 0;

//------ DiscardSavedData make a Refresh in your data -------
$creport->DiscardSavedData;
$creport->ReadRecords();

//------ Pass formula fields --------
// $creport->FormulaFields->Item(1)->Text = ("'My Report Title'");
$creport->ParameterFields(1)->AddCurrentValue ("Hello World");   // <-- param 1
$creport->ParameterFields(2)->AddCurrentValue (123); // <-- param 2

//------ Export to PDF -------
$creport->ExportOptions->DiskFileName=$my_pdf;
$creport->ExportOptions->FormatType=31;
$creport->ExportOptions->DestinationType=1;
$creport->Export(false);

//------ Release the variables ------
$creport = null;
$crapp = null;
$ObjectFactory = null;

//------ Embed the report in the webpage ------
print "<embed src=\"MyReport.pdf\" width=\"100%\" height=\"100%\">"

?>

在CR中,在Field Explorer中,我右键单击Parameter Fields并选择New。但是,我不知道要分配的参数字段名称和其他设置是什么,以便在上面的代码中获取param1和param2值。

帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

$ creport-&gt; ParameterFields(1) - &gt; SetCurrentValue(“Hello World”); //&lt; - param 1

$ creport-&gt; ParameterFields(2) - &gt; SetCurrentValue(123); //&lt; - param 2

希望这会对你有所帮助。