示例#1
0
        public void Import(ScriptableObject sObj, Type textType, ScriptDefine scriptDefine)
        {
            FieldInfo fi = textType.GetField(fieldName);

            // Is a List<...> filed
            if (fi != null && fi.FieldType.IsGenericType && fi.FieldType.GetGenericTypeDefinition() == typeof(List <>))
            {
                // new List<'Data'>()
                var container = Activator.CreateInstance(fi.FieldType);
                fi.SetValue(sObj, container);

                Type entityType = fi.FieldType.GetGenericArguments()[0];

                // Find List.Add(...)
                MethodInfo mi = fi.FieldType.GetMethod("Add", new Type[] { entityType });
                if (mi != null)
                {
                    foreach (ICell cell in cellList)
                    {
                        IRow row = cell.Row;

                        // find this 'Data' class define
                        ClassDefine cd = scriptDefine.classDefines.Find((x) => x.className.Replace("class ", string.Empty) == elementTypeName);
                        if (cd != null && row != null)
                        {
                            // new a 'Data' class
                            var element = Activator.CreateInstance(entityType);

                            // for each fileds in this class
                            for (int i = 0; i < cd.fields.Count; i++)
                            {
                                FieldDefine assetField = cd.fields[i];
                                assetField.Import(element, entityType, i, row, cd);
                            }

                            // List.Add(element)
                            mi.Invoke(container, new object[] { element });
                        }
                    }
                }
            }
            else
            {
            }
        }
示例#2
0
    private static void ImportSpreadSheet(string path, bool script)
    {
        // combine full file path
        string        fullPath = Application.dataPath;
        DirectoryInfo di       = Directory.GetParent(fullPath);

        fullPath = Path.Combine(di.ToString(), path);
        //Debug.Log(fullPath);

        using (Stream stream = new FileStream(fullPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
        {
            IWorkbook workbook = null;

            try
            {
                if (fullPath.EndsWith(".xls", System.StringComparison.InvariantCulture))
                {
                    workbook = new HSSFWorkbook(stream);
                }
                else if (fullPath.EndsWith(".xlsx", System.StringComparison.InvariantCulture))
                {
                    workbook = new XSSFWorkbook(stream);
                }
                else
                {
                    stream.Close();
                    return;
                }
            }
            catch (System.Exception e)
            {
                Debug.LogException(e);
            }

            // for each sheet
            IEnumerator sheetEnum = workbook.GetEnumerator();
            while (sheetEnum.MoveNext())
            {
                var sheet = sheetEnum.Current as ISheet;
                //Debug.Log(sheet.SheetName);

                ScriptDefine scriptDefine = new ScriptDefine()
                {
                    filename = sheet.SheetName,
                };

                // for each row
                IEnumerator rowEnum = sheet.GetRowEnumerator();
                while (rowEnum.MoveNext())
                {
                    var row = rowEnum.Current as IRow;
                    if (row != null)
                    {
                        // for each cell
                        IEnumerator cellEnum = row.GetEnumerator();
                        while (cellEnum.MoveNext())
                        {
                            var cell = cellEnum.Current as ICell;
                            if (cell != null)
                            {
                                if (!scriptDefine.Input(cell))
                                {
                                    continue;
                                }
                                //Debug.LogFormat("{0},{1}={2}.",cell.RowIndex, cell.ColumnIndex, cell.ToString());
                            }
                        }
                    }
                }

                if (!script)
                {
                    scriptDefine.Import();
                }
                else
                {
                    scriptDefine.GenerateClass();
                }
            } // for each Sheet

            // close file stream
            stream.Close();
        }
    }