CR:CrystalReportViewer

        //报表对象
        ReportDocument rptDoc = new ReportDocument();

 

        protected void Page_Load(object sender, EventArgs e)
        {           

            // 数据库登陆信息
            TableLogOnInfo logInfo = new TableLogOnInfo();

            // 连接字符
            string strConn = ConfigurationManager.ConnectionStrings["DataSqlConnection"].ConnectionString;

            string[] parma = strConn.Split(';');

            // 设置数据连接
            for (int index = 0; index < parma.Length; index++)
            {
                if (parma[index].Contains("server"))
                {
                    logInfo.ConnectionInfo.ServerName = parma[index].Split('=')[1];
                }
                else if (parma[index].Contains("database"))
                {
                    logInfo.ConnectionInfo.DatabaseName = parma[index].Split('=')[1];
                }
                else if (parma[index].Contains("user id"))
                {
                    logInfo.ConnectionInfo.UserID = parma[index].Split('=')[1];
                }
                else if (parma[index].Contains("password"))
                {
                    logInfo.ConnectionInfo.Password = parma[index].Split('=')[1];
                }
            }


            //报表名称
            string strRpt = "报表名称";

            // 报表地址
            string strPath = Server.MapPath(strRpt + ".rpt");

            // 读取报表
            rptDoc.Load(strPath);

            // 设置报表变量
            SetParamFields(strRpt);


            // 设置登陆信息
            rptDoc.Database.Tables[0].ApplyLogOnInfo(logInfo);

            // 设置数据
            rptView.ReportSource = rptDoc;
        }

 

        //注销报表
        private void Page_Unload(object sender, EventArgs e)
        {
            rptDoc.Close();
            rptDoc.Dispose();
        }

 

        /// <summary>
        /// 设置参数
        /// </summary>
        /// <param name="strRptName"></param>
        private void SetParamFields(string strRptName)
        {
            Hashtable paramList = getParamsList(strRptName);

            if (paramList.Count > 0)
            {
                // 变量
                ParameterFields paramFields = new ParameterFields();

                IDictionaryEnumerator it = paramList.GetEnumerator();

                while (it.MoveNext())
                {
                    // 变量Field
                    ParameterField field = new ParameterField();

                    field.ParameterFieldName = it.Key.ToString();

                    // 变量值
                    ParameterDiscreteValue value = new ParameterDiscreteValue();

                    value.Value = Request.QueryString[it.Key.ToString()];

                    field.CurrentValues.Add(value);

                    paramFields.Add(field);
                }

                // 设置变量
                rptView.ParameterFieldInfo = paramFields;
            }
        }

 

        public static Hashtable getParamsList(string strRptID)
        {
            Hashtable paramsList = new Hashtable();

            try
            {
                string filename = HttpContext.Current.Server.MapPath("RptParamFile.xml");
                if (!File.Exists(filename)) throw new Exception("xmltext's file not found!");

                XmlDocument xdoc = new XmlDocument();

                xdoc.Load(filename);

                string strNodepath = "/ReportID/" + strRptID + "/params";

                XmlNode child = xdoc.SelectSingleNode(strNodepath);

                if (child != null)
                {
                    foreach (XmlNode node in child.ChildNodes)
                    {
                        paramsList.Add(node.Attributes["paramName"].Value, node.Attributes["type"].Value);
                    }
                }
            }
            catch
            {

            }
            return paramsList;
        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值