コード例 #1
0
        public FlatDataModel flhz(List <string> classifynames, params FLHZ_OPERATION[] par)
        {
            FlatDataModel           t_fdm   = (FlatDataModel)this.Clone();
            List <List <DataUnit> > bunches = t_fdm.make_bunch(classifynames);

            foreach (FLHZ_OPERATION item in par)
            {
                //先检查fieldname是否合法
                if (!t_fdm.vn.Contains(item.fieldname))
                {
                    throw new ArgumentException(string.Format("不存在字段名{0}", item.fieldname));
                }

                //处理newname的默认值
                if (item.newname.Length == 0)
                {
                    item.newname = item.fieldname;
                }
            }

            //开始统计
            FlatDataModel rt = new FlatDataModel();

            foreach (List <DataUnit> thisbunch in bunches)
            {
                //先写入classfynames
                DataUnit du = new DataUnit(rt);
                foreach (string n in classifynames)
                {
                    du.data.Add(n, thisbunch[0].data[n]);
                }


                //再写入统计数据
                foreach (FLHZ_OPERATION op in par)
                {
                    //把List<DataUnit>中fieldname提取出来
                    List <object> lo = new List <object>();
                    foreach (DataUnit u in thisbunch)
                    {
                        lo.Add(u.data[op.fieldname]);
                    }
                    //统计
                    du.data.Add(op.newname, op.func(lo));
                }
                rt.units.Add(du);
            }

            //整理vn
            foreach (string item in classifynames)
            {
                rt.vn.Add(item);
            }
            foreach (FLHZ_OPERATION item in par)
            {
                rt.vn.Add(item.newname);
            }
            return(rt);
        }
コード例 #2
0
        /// <summary>
        /// 浅拷贝
        /// 如果是字符串和数 拷贝后与源完全独立
        /// 如果是对象 就是互连的
        /// </summary>
        /// <returns></returns>
        public object Clone()
        {
            FlatDataModel fdm = new FlatDataModel();

            this.vn.ForEach(i => fdm.vn.Add(i));//复制vn
            DataUnit u;

            foreach (DataUnit item in this)
            {
                u     = (DataUnit)item.Clone();
                u.fdm = fdm;
                fdm.units.Add(u);
            }

            return(fdm);
        }
コード例 #3
0
 public DataUnit(FlatDataModel f)
 {
     this.fdm = f;
 }
コード例 #4
0
        /// <summary>
        /// 从其他模型中添加字段
        /// </summary>
        /// <param name="link_field">连接字段</param>
        /// <param name="add_fields">添加字段</param>
        /// <param name="other_model">其他模型</param>
        /// <param name="default_value">默认值,在找不到对应dataunit的时候使用</param>
        public void add_field_from_other_model(string link_field, string[] add_fields, FlatDataModel other_model, object default_value)
        {
            //检查字段名的合法性
            if (!this.vn.Contains(link_field) || !other_model.vn.Contains(link_field))
            {
                throw new ArgumentException(string.Format("字段名{0}不存在。", link_field));
            }
            foreach (var item in add_fields)
            {
                if (!other_model.vn.Contains(item))
                {
                    throw new ArgumentException(string.Format("字段名{0}不存在。", item));
                }
                if (this.vn.Contains(item))
                {
                    throw new ArgumentException(string.Format("字段名{0}已经存在。", item));
                }
            }

            //开始连接
            foreach (DataUnit u in this)
            {
                var umatch = other_model.find_one(delegate(DataUnit tu)
                {
                    dynamic d1 = tu.data[link_field];
                    dynamic d2 = u.data[link_field];
                    return(d1 == d2);
                });
                if (null == umatch)
                {
                    //没找到
                    foreach (string item in add_fields)
                    {
                        u.data.Add(item, default_value);
                    }
                }
                else//找到
                {
                    foreach (string item in add_fields)
                    {
                        u.data.Add(item, umatch.data[item]);
                    }
                }
            }

            //更新vn
            foreach (string item in add_fields)
            {
                this.vn.Add(item);
            }
        }
コード例 #5
0
        /// <summary>
        /// 从excel文件中载入
        /// 第一行作为vn 后续行作为数据
        /// </summary>
        /// <param name="pathname"></param>
        /// <param name="sheetname"></param>
        /// <returns></returns>
        public static FlatDataModel load_from_excel(string pathname, string sheetname = null)
        {
            //create the Application object we can use in the member functions.
            Microsoft.Office.Interop.Excel.Application _excelApp = new Microsoft.Office.Interop.Excel.Application();
            _excelApp.Visible = false;

            string fileName = pathname;

            //open the workbook
            Workbook workbook = _excelApp.Workbooks.Open(fileName,
                                                         Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                                         Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                                         Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                                         Type.Missing, Type.Missing);

            //select the first sheet
            Worksheet worksheet;

            if (null == sheetname)
            {
                worksheet = (Worksheet)workbook.Worksheets[1];
            }
            else
            {
                worksheet = (Worksheet)workbook.Worksheets[sheetname];
            }


            //find the used range in worksheet
            Microsoft.Office.Interop.Excel.Range excelRange = worksheet.UsedRange;

            //get an object array of all of the cells in the worksheet (their values)
            object[,] valueArray = (object[, ])excelRange.get_Value(
                XlRangeValueDataType.xlRangeValueDefault);

            //access the cells 创建flatdatamodel
            FlatDataModel fdm = new FlatDataModel();

            for (int row = 1; row <= worksheet.UsedRange.Rows.Count; ++row)
            {
                DataUnit du = new DataUnit(fdm);
                for (int col = 1; col <= worksheet.UsedRange.Columns.Count; ++col)
                {
                    //access each cell
                    object thiscell = valueArray[row, col];
                    if (row == 1)//第一行 设定vn
                    {
                        fdm.vn.Add(thiscell.ToString());
                    }
                    else
                    {//其他行 赋值du
                        du.data.Add(fdm.vn[col - 1], thiscell);
                    }
                    //System.Diagnostics.Debug.Print(valueArray[row, col].ToString());
                }
                if (du.data.Count != 0)
                {
                    fdm.units.Add(du);
                }
            }

            //clean up stuffs
            workbook.Close(false, Type.Missing, Type.Missing);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);

            _excelApp.Quit();
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(_excelApp);

            return(fdm);
        }