本文主要是介绍crystalReport(水晶报表)动态输入参数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
命名空间为:using CrystalDecisions.Sharedprivate void BuildReportViewerParameters(CrystalDecisions.Web.CrystalReportViewer rptViewer, Hashtable ParamTable)
{
//定义报表视图的参数的方法
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.ToString();
ParamField.CurrentValues = ParamValues;
ParamFields.Add(ParamField);
}
rptViewer.ParameterFieldInfo = ParamFields;
}
private void BuildReportClassParameters(CrystalDecisions.CrystalReports.Engine.ReportClass rpt, Hashtable ParamTable)
{
//定义报表类的参数的方法
foreach (object key in ParamTable.Keys)
{
ParameterValues ParamValues = new ParameterValues();
ParameterDiscreteValue ParamValue = new ParameterDiscreteValue();
ParamValue.Value = ParamTable[key].ToString();
ParamValues.Add(ParamValue);
rpt.DataDefinition.ParameterFields[key.ToString()].ApplyCurrentValues(ParamValues);
}
}
protected void BuildReportParameters(CrystalDecisions.Web.CrystalReportViewer rpt,Hashtable ParamTable)
{
//定义报表参数的方法
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;
}
然后再你的需要调用参数的地方,加如下引用:
private void Button_output_Click(object sender, System.EventArgs e)
{
CrystalDecisions.Shared.TableLogOnInfo logInfo = new CrystalDecisions.Shared.TableLogOnInfo(); //设置报表的登录信息
logInfo.ConnectionInfo.ServerName = "ANGEL"; //设置报表与库的连接信息
logInfo.ConnectionInfo.DatabaseName = "KaoQin";
logInfo.TableName = "Emstatistic";
logInfo.ConnectionInfo.UserID = "sa";
logInfo.ConnectionInfo.Password = "";
CrystalReportViewer1.ReportSource = Server.MapPath("cr_aa.rpt"); //指定报表的数据源
CrystalReportViewer1.LogOnInfo.Add(logInfo);//对参数进行赋什值
// CrystalReportViewer1.DataBind();
//声明变量并获取导出选项
cr_aa myReport = new cr_aa(); //定义一个名为myReport的报表
myReport.Database.Tables[0].ApplyLogOnInfo(logInfo);
CrystalReportViewer1.ReportSource = myReport;
CrystalReportViewer1.DataBind();
Hashtable t = new Hashtable();
t.Add("p_st_month", Textmonth.Text); //为参数赋值
BuildReportParameters(CrystalReportViewer1, t); //调用建立报表参数的方法
BuildReportViewerParameters(CrystalReportViewer1, t); //给报表浏览器添加参数
BuildReportClassParameters(myReport, t); //给报表类添加参数
CrystalDecisions.Shared.DiskFileDestinationOptions diskOpts = new CrystalDecisions.Shared.DiskFileDestinationOptions();
//设置导出格式
myReport.ExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;
myReport.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.Excel;
//设置磁盘文件选项
diskOpts.DiskFileName = "C://Documents and Settings//Administrator//My Documents//kaoqin_rpt.xls";
myReport.ExportOptions.DestinationOptions = diskOpts;
myReport.Export();
Response.Write("<script language='javascript'>alert('您已经将报表成功输出到Excel文件'); window.location.href='customRpt.aspx';</" + "script>");
}
这篇关于crystalReport(水晶报表)动态输入参数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!