在crystalReport(水晶報表)動態輸入參數(轉)

來源:互聯網
上載者:User
動態|水晶報表 命名空間為: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.href='customRpt.aspx';</" + "script>");
        }

相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。