在crystalReport(水晶报表)动态输入参数(转)

80酷酷网    80kuku.com

  动态|水晶报表命名空间为:using CrystalDecisions.Shared        

private 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.

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: