Dynamic input parameters in CrystalReport (Crystal Report)

xiaoxiao2021-03-06  50

Named space is: use crestaldecisions.shared

Private Void BuildReportViewerParameters (Crystaldecisions.Web.crystalReportViewer Rptviewer, Hashtable ParamTable)

{

/ / Define the method of the parameter of the report view

Parameterfields paramfields = new parameterfields ();

Foreach (Object Key in paramtable.keys)

{

Parameterfield paramfield = new parameterfield ();

Parametervalues ​​paramvalues ​​= new parameterValues ​​();

ParameterDiscretevalue paramvalue = new parameterdiscretevalue ();

ParamValue.Value = paramtable [key] .tostring ();

Paramvalues.Add (paramvalue);

Paramfield.ParameterfieldName = key.toT7tring ();

Paramfield.currentVale = paramvalues;

Paramfields.Add (paramfield);

}

RPtViewer.ParameterfieldInfo = paramfields;

}

Private void BuildReportClassparameters (Crystaldecisions.crystalreports.Engine.ReportClass RPT, Hashtable ParamTable)

{

/ / Define the method of parameters of the report class

Foreach (Object Key in paramtable.keys)

{

Parametervalues ​​paramvalues ​​= new parameterValues ​​();

ParameterDiscretevalue paramvalue = new parameterdiscretevalue ();

ParamValue.Value = paramtable [key] .tostring ();

Paramvalues.Add (paramvalue);

RPT.DATADEFINITION.PARAMELDS [Key.toTString ()]. ​​ApplyCurrentValues ​​(Paramvalues);

}

}

Protected Void BuildReportParameters (Crystaldecisions.Web.crystalReportViewer RPT, Hashtable ParamTable)

{

/ / Define how to report parameters

Parameterfields paramfields = new parameterfields ();

Foreach (Object Key in paramtable.keys)

{

Parameterfield Paramfield;

Parametervalues ​​Paramvalues;

ParameterDiscretevalue Paramvalue;

Paramfield = new parameterfield ();

Paramvalues ​​= new parametervalues ​​();

Paramvalue = new parameterdiscretevalue ();

ParamValue.Value = (string) paramtable [key];

Paramvalues.Add (paramvalue);

Paramfield.ParameterfieldName = (String) Key; paramfield.currentValues ​​= paramvalues

Paramfields.Add (paramfield);

}

Rpt.ParameterfieldInfo = paramfields;

}

Then you need to call the parameters, add the reference:

Private void button_output_click (Object Sender, System.EventArgs E)

{

Crystaldecisions.Shared.TablesLogonInfo loginfo = new crystaldecisions.shared.tablelogonInfo (); // Set the login information of the report

Loginfo.connectioninfo.servername = "angel"; // Set the connection information of the report to the library

Loginfo.connectionInfo.databaseName = "kaoqin";

Loginfo.tablenaMe = "emstatistic";

Loginfo.connectioninfo.userid = "sa";

Loginfo.connectioninfo.password = "";

CrystalReportViewer1.Reportsource = Server.mAppath ("cr_aa.rpt"); / / Specify the data source of the report

CrystalReportViewer1.logoninfo.add (loginfo); // Trusted the parameter

// CrystalReportViewer1.databind ();

// Declare the variable and get the export option

Cr_aa myreport = new cr_aa (); // Define a report called MyReport

MyReport.Database.tables [0] .applylogoninfo (loginfo);

CrystalReportViewer1.Reportsource = MyReport;

CrystalReportViewer1.DATABIND ();

Hashtable t = new hashtable ();

T.ADD ("p_st_month", textmonth.text); / / assigns parameter

BuildReportParameters (CrystalReportViewer1, T); // Call the method of establishing a report parameter

BuildReportViewerParaseters (CrystalReportViewer1, T); // Add parameters to report browsers

BuildReportClassParameters (MyReport, T); // Add parameters to report class

CrystalDecisions.Shared.diskFileDestinationOptions Diskopts = new crystaldecisions.Shared.diskFileDestinationOptions ();

/ / Set export format

MyReport.ExportOptions.exportDestinationType = Crystaldecisions.Shared.ExportDestinationType.DiskFile;

MyReport.ExportOptions.exportFormattype = Crystaldecisions.Shared.ExportFormattype.excel;

/ / Set disk file option

Diskopts.diskFileName = "c: // Documents and settings // administrator // my documents // kaoqin_rpt.xls"; myreport.exportOptions.DestinationOptions = Diskopts;

myreport.export ();

Response.write ("