Ejemplo n.º 1
0
        public object GetGenericValue(ICell cell, TypeInfo type)
        {
            if (ReadHelper.IsLinkCell(cell))
            {
                switch (type.genericType.sign)
                {
                case TypeInfo.Sign.Array:
                    return(ReadLinkArray(cell, type.genericArguments[0]));

                case TypeInfo.Sign.List:
                    return(ReadLinkList(cell, type.genericArguments[0]));

                case TypeInfo.Sign.Dictionary:
                    return(ReadLinkDict(cell, null));

                default:
                    return(null);
                }
            }
            else
            {
                if (cell != null)
                {
                    //as json data
                    return(Newtonsoft.Json.JsonConvert.DeserializeObject(cell.StringCellValue, type.ToSystemType()));
                }
                else
                {
                    return(null);
                }
            }
        }
Ejemplo n.º 2
0
        public IDictionary ReadDictionary(ISheet sheet, Schema schema, string keyField, int dataStart, int colStart, int colEnd, List <string> header, bool removeKeyInElement = false, int dataEnd = -1)
        {
            if (header == null || header.Count == 0)
            {
                header = ReadHelper.GetHeader(sheet, 0, colStart, colEnd);
            }

            List <Field> headerFields = ReadHelper.PrepareHeaderFields(header, schema);

            //如果没指定key,则默认使用第一个
            if (string.IsNullOrEmpty(keyField))
            {
                keyField = header[0];
            }

            Dictionary <object, object> dict = new Dictionary <object, object>();
            int l = dataEnd <= 0 ? sheet.LastRowNum : (dataEnd < sheet.LastRowNum ? dataEnd : sheet.LastRowNum);

            for (int i = sheet.FirstRowNum + dataStart; i <= l; ++i)
            {
                Dictionary <string, object> record = ReadRowData(sheet.GetRow(i), headerFields, colStart, colEnd);
                object key = record[keyField];
                dict[key] = record;
                if (removeKeyInElement)
                {
                    record.Remove(keyField);
                }
            }
            return(dict);
        }
Ejemplo n.º 3
0
 public object GetObjectValue(ICell cell, TypeInfo type)
 {
     if (ReadHelper.IsLinkCell(cell))
     {
         return(ReadLinkObject(cell, type));
     }
     else
     {
         //as json data
         return(Newtonsoft.Json.JsonConvert.DeserializeObject(cell.StringCellValue, type.ToSystemType()));
     }
 }
Ejemplo n.º 4
0
        public void WriteList(ISheet sheet, Schema schema, IList list, int dataStart, int colStart, List <string> header)
        {
            List <Field> headerFields = header == null ? schema.fields:ReadHelper.PrepareHeaderFields(header, schema);

            for (int i = 0, l = list.Count; i < l; ++i)
            {
                IRow row = sheet.GetRow(i + dataStart);
                if (row == null)
                {
                    row = sheet.CreateRow(i + dataStart);
                }
                WriteRowData(row, list[i], headerFields, colStart);
            }
        }
Ejemplo n.º 5
0
        public void WriteDictionary(ISheet sheet, Schema schema, IDictionary data, int dataStart, int colStart, List <string> header)
        {
            List <Field> headerFields = header == null ? schema.fields : ReadHelper.PrepareHeaderFields(header, schema);
            int          i            = dataStart;

            foreach (var iter in data.Values)
            {
                IRow row = sheet.GetRow(i);
                if (row == null)
                {
                    row = sheet.CreateRow(i);
                }
                WriteRowData(row, iter, headerFields, colStart);
                ++i;
            }
        }
Ejemplo n.º 6
0
        public List <object> ReadList(ISheet sheet, Schema schema, int dataStart, int dataEnd, int colStart, int colEnd, List <string> header)
        {
            if (header == null || header.Count == 0)
            {
                header = ReadHelper.GetHeader(sheet, 0, colStart, colEnd);
            }

            List <Field> headerFields = ReadHelper.PrepareHeaderFields(header, schema);

            List <object> list = new List <object>();
            int           l    = dataEnd <= 0 ? sheet.LastRowNum : (dataEnd < sheet.LastRowNum ? dataEnd : sheet.LastRowNum);

            for (int i = sheet.FirstRowNum + dataStart; i <= l; ++i)
            {
                Dictionary <string, object> record = ReadRowData(sheet.GetRow(i), headerFields, colStart, colEnd);
                list.Add(record);
            }
            return(list);
        }
Ejemplo n.º 7
0
        public object GetCellValue(ICell cell, TypeInfo dataType)
        {
            switch (dataType.sign)
            {
            case TypeInfo.Sign.Byte:
                return(ReadHelper.GetIntValue(cell));

            case TypeInfo.Sign.Int:
                return(ReadHelper.GetIntValue(cell));

            case TypeInfo.Sign.Float:
                return(ReadHelper.GetFloatValue(cell));

            case TypeInfo.Sign.Long:
                return(ReadHelper.GetLongValue(cell));

            case TypeInfo.Sign.Double:
                return(ReadHelper.GetDoubleValue(cell));

            case TypeInfo.Sign.Boolean:
                return(ReadHelper.GetBoolValue(cell));

            case TypeInfo.Sign.String:
                return(ReadHelper.GetStringValue(cell));

            case TypeInfo.Sign.Array:
            case TypeInfo.Sign.List:
            case TypeInfo.Sign.Dictionary:
                return(GetCompositeValue(cell, dataType));

            case TypeInfo.Sign.Generic:
                return(GetGenericValue(cell, dataType));

            case TypeInfo.Sign.Object:
                return(GetObjectValue(cell, dataType));

            default:
                break;
            }
            return(null);
        }
Ejemplo n.º 8
0
        public static Schema ReadSchema(ISheet sheet, HeadModel headModel, int schemaColOffset = 0)
        {
            Schema schema = new Schema();

            schema.name = sheet.SheetName;

            //first row is name
            IRow NameRow = sheet.GetRow(sheet.FirstRowNum + headModel.NameRow);
            //second row is data type
            IRow typeRow = sheet.GetRow(sheet.FirstRowNum + headModel.DataTypeRow);

            bool haveDescription = headModel.DescriptionRow != -1;
            bool haveSide        = headModel.SideRow != -1;
            //thirdrow is description
            IRow descriptionRow = null;

            if (haveDescription)
            {
                descriptionRow = sheet.GetRow(sheet.FirstRowNum + headModel.DescriptionRow);
            }

            IRow sideRow = null;

            if (haveSide)
            {
                sideRow = sheet.GetRow(sheet.FirstRowNum + headModel.SideRow);
            }

            for (int i = NameRow.FirstCellNum + schemaColOffset; i < NameRow.LastCellNum; ++i)
            {
                //get name
                ICell  nameCell = NameRow.GetCell(i);
                string name     = nameCell.StringCellValue;

                //get type
                TypeInfo dataType = TypeInfo.Object;
                ICell    typeCell = typeRow.GetCell(i);
                if (typeCell != null)
                {
                    dataType = TypeInfo.Parse(typeCell.StringCellValue);
                }

                //get description
                string description = "";
                if (haveDescription)
                {
                    ICell descriptionCell = descriptionRow.GetCell(i);
                    description = descriptionCell.StringCellValue;
                }

                //get comment
                string comment = "";
                if (nameCell.CellComment != null)
                {
                    comment = nameCell.CellComment.String.String;
                }

                Side side = Side.All;
                if (haveSide)
                {
                    ICell  sideCell  = sideRow.GetCell(i);
                    string sideValue = ReadHelper.GetStringValue(sideCell);
                    if (!string.IsNullOrEmpty(sideValue))
                    {
                        side = (Side)System.Enum.Parse(typeof(Side), sideValue);
                    }
                }

                Field field = new Field(name, dataType, comment, description, side);
                schema.AddField(field);
            }

            return(schema);
        }