您的位置:首页 > 其它

sharepoint 查询统计list数据,使用CAML联合datatable方法

2013-06-04 11:02 387 查看
       需求;根据审批结果,统计上月个人,部门。财务已批准和未批准的综合。各种复杂技术不赘述。

      新浪微博   jasondct 

        /// <summary>

        /// 计算时间范围内各类审批统计

        /// </summary>

        /// <param name="monthfirst">月处</param>

        /// <param name="monthend">月末</param>

        protected void GetCostBudge(DateTime monthfirst, DateTime monthend)

        {

            //转换时间

            string monthfirst1 = SPUtility.CreateISO8601DateTimeFromSystemDateTime(monthfirst);

            string monthend1 = SPUtility.CreateISO8601DateTimeFromSystemDateTime(monthend);

            //获得当前列表

            using (SPSite site = new SPSite(SPContext.Current.Site.ID))

            {

                using (SPWeb web = site.OpenWeb("expense"))

                {

                    SPList list = web.Lists["EBRL"];

                    SPQuery Query = new SPQuery();

                    Query.Query = string.Format(@"<Where><And><Geq><FieldRef Name='SubmitTime' /><Value IncludeTimeValue='TRUE' Type='DateTime'>{0}</Value>

                    </Geq><Leq><FieldRef Name='SubmitTime' /><Value IncludeTimeValue='TRUE' Type='DateTime'>{1}</Value></Leq></And></Where>",monthfirst1, monthend1 );

                    Query.ViewFields = @"<FieldRef Name='FreeType' />

                                            <FieldRef Name='Total'/>

                                            <FieldRef Name='State'/> 

                                            <FieldRef Name='Dep'/>   

                                             <FieldRef Name='DepID'/>   

                                            <FieldRef Name='ApplyName'/> ";

                    DataTable mydt = list.GetItems(Query).GetDataTable();

                    if (mydt != null)

                    {

                        //先得到个人总数   txtUntreatedGross

                        txtUsedGross.Text = mydt.Compute("Sum(Total)", "State = 1 and ApplyName ='"+labUserName.Text+"'").ToString();

                        txtUntreatedGross.Text = mydt.Compute("Sum(Total)", "State = 0 and ApplyName='" + labUserName.Text + "'").ToString();

                       

                        //再获取个人费用类型内的总数

                        txtMonth.Text = mydt.Compute("Sum(Total)", "State = 1 and ApplyName ='" + labUserName.Text + "' and FreeType='" + labListName.Text + "'").ToString();

                        txtUntreated.Text = mydt.Compute("Sum(Total)", "State = 0 and ApplyName ='" + labUserName.Text + "' and FreeType='" + labListName.Text + "'").ToString();

                        //如果是xx部门只有二级部门

                        string[] getFunctionDep = labDep.Text.Split('-');

                        if (getFunctionDep[0].Contains("xx") && getFunctionDep.Length>1)

                        {

                            string FunctionDep = GetDepID(getFunctionDep[0]) + "-" + GetDepID(labDep.Text.Trim());

                            //二级部门已处理  Dep

                          txtUsedTwo.Text = mydt.Compute("Sum(Total)", "State = 1 and DepID ='" + FunctionDep + "' and FreeType='" + labListName.Text + "'").ToString();

                            //二级部门待处理

                          txtUntreatedTwo.Text = mydt.Compute("Sum(Total)", "State = 0 and DepID ='" + FunctionDep + "' and FreeType='" + labListName.Text + "'").ToString();

                          //txtUsedTwo.Text = mydt.Compute("Sum(Total)", "State = 1 and DepID ='" + FunctionDep + "' and FreeType='" + labListName.Text + "'").ToString();

                          //txtUntreatedOne.Text = mydt.Compute("Sum(Total)", "State = 0 and DepID ='" + FunctionDep + "' and FreeType='" + labListName.Text + "'").ToString();

                          //得到总数   

                          txtUsedGross.Text = mydt.Compute("Sum(Total)", "State = 1 and DepID ='" + FunctionDep + "'").ToString();

                          txtUntreatedGross.Text = mydt.Compute("Sum(Total)", "State = 0 and DepID ='" + FunctionDep + "'").ToString();

                        }

                        else

                        {

                            //根据登录者部门获得三级部门

                            //逐级获得部门ID并赋值给

                          

                            //给一级部门赋值  txtUsedUnit

                            if (!labDep.Text.Contains("-"))

                            {

                                //获得DepID

                                string ItemDepID = "";

                                string DepID = GetDepID(labDep.Text);

                                //一级部门已处理

                                txtUsedUnit.Text = mydt.Compute("Sum(Total)", "State = 1 and substring(DepID,1,6) ='" + DepID + "' and FreeType='" + labListName.Text + "'").ToString();

                                //一级部门待处理

                                txtUntreatedOne.Text = mydt.Compute("Sum(Total)", "State = 0 and substring(DepID,1,6) ='" + DepID + "' and FreeType='" + labListName.Text + "'").ToString();

                                //得到总数   

                                txtUsedGross.Text = mydt.Compute("Sum(Total)", "State = 1 and substring(DepID,1,6) ='" + DepID + "'").ToString();

                                txtUntreatedGross.Text = mydt.Compute("Sum(Total)", "State = 0 and substring(DepID,1,6) ='" + DepID + "'").ToString();

                            }

                            else

                            {

                                string[] myarr = labDep.Text.Split('-');

                                //定义一个公共值,首先获取此人是一级部门

                                string recursionDep = string.Empty;

                                string recursionDep1 =GetDepID( myarr[0]);

                                recursionDep = recursionDep1;

                                //一级部门已处理

                                txtUsedUnit.Text = mydt.Compute("Sum(Total)", "State = 1 and substring(DepID,1,6) ='" + recursionDep + "' and FreeType='" + labListName.Text + "'").ToString();

                                //一级部门待处理

                                txtUntreatedOne.Text = mydt.Compute("Sum(Total)", "State = 0 and substring(DepID,1,6) ='" + recursionDep + "' and FreeType='" + labListName.Text + "'").ToString();

                                //得到总数   

                                txtUsedGross.Text = mydt.Compute("Sum(Total)", "State = 1 and substring(DepID,1,6) ='" + recursionDep + "'").ToString();

                                txtUntreatedGross.Text = mydt.Compute("Sum(Total)", "State = 0 and substring(DepID,1,6) ='" + recursionDep + "'").ToString();

                                ///////////////////////////////////////////////////////TWO////////////////////////////////////////////////

                                string recursionDep2 =GetDepID( myarr[0]+"-"+ myarr[1]);

                                recursionDep += "-" + recursionDep2;

                                //二级部门已处理  Dep

                                txtUsedTwo.Text = mydt.Compute("Sum(Total)", "State = 1 and substring(DepID,1,13) ='" + recursionDep + "' and FreeType='" + labListName.Text + "'").ToString();

                                //二级部门待处理

                                txtUntreatedTwo.Text = mydt.Compute("Sum(Total)", "State = 0 and substring(DepID,1,13) ='" + recursionDep + "' and FreeType='" + labListName.Text + "'").ToString();

                                ////////////////////////////////////////////////////three/////////////////////////////////////////////////

                                try

                                {

                                    string recursionDep3 = GetDepID(myarr[0]+"-"+ myarr[1]+"-"+myarr[2]);

                                    recursionDep += "-" + recursionDep3;

                                    txtUsedthree.Text = mydt.Compute("Sum(Total)", "State = 1 and DepID ='" + recursionDep + "' and FreeType='" + labListName.Text + "'").ToString();

                                    txtUntreatedthree.Text = mydt.Compute("Sum(Total)", "State = 0 and DepID ='" + recursionDep + "' and FreeType='" + labListName.Text + "'").ToString();

                                }

                                catch

                                { 

                                }

                            }

                        }

                    }

                }

            }

        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐