首页 > 开发 > 综合 > 正文

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

2024-07-21 02:23:53
字体:
来源:转载
供稿:网友
命名空间为: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>");
        }
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表