CR:CrystalReportViewer

来源:互联网 发布:net.sf.json 编辑:程序博客网 时间:2024/06/06 00:29

        //报表对象
        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;
        }

原创粉丝点击