Beispiel #1
0
        public void Init(string filePath)
        {
            this._filePath = filePath;
            dataDict.Clear();
            keyColumnNameList.Clear();
            string content = FileUtilCat.ReadUnityFile(filePath);

            //读取每一行的内容
            //window用"\r\n"
            string[] lines = content.Split("\r\n".ToCharArray());
            //string[] lineArray = textAsset.text.Split("\r".ToCharArray());
            dataRowCount = lines.Length - 1;

            string[] columnNames = lines[0].Split(',');

            for (int i = 0; i < columnNames.Length; i++)
            {
                string columnName = columnNames[i];
                int    keyIndex   = GetKeyIndex(columnName);
                if (keyIndex != -1)
                {
                    columnName = columnName.Substring(0, keyIndex);
                    keyColumnNameList.Add(columnName);
                    columnNames[i] = columnName;
                }
            }

            for (int i = 1; i < lines.Length; i++)
            {
                string[] rowContents = lines[i].Split(',');
                if (rowContents[0] == "\n" || rowContents[0] == "")
                {
                    dataRowCount = dataRowCount - 1;
                    continue;
                }

                LinkedDictionary <string, string> lineDataDict = new LinkedDictionary <string, string>();
                string keys = "";

                for (int j = 0; j < columnNames.Length; j++)
                {
                    bool isKey = keyColumnNameList.Contains(columnNames[j]);
                    lineDataDict[columnNames[j]] = rowContents[j];
                    if (isKey)
                    {
                        if (keys == "")
                        {
                            keys = rowContents[j];
                        }
                        else
                        {
                            keys += "_" + rowContents;
                        }
                    }
                }

                dataDict[keys] = lineDataDict;
            }
        }
Beispiel #2
0
 /// <summary>
 /// 设置assetData
 /// </summary>
 /// <param name="data"></param>
 public void SetAssetData(LinkedDictionary <string, ExcelRow> data)
 {
     this.dataDict = new Dictionary <string, object>();
     this.dataSourceItemListDict = null;
     this.idList    = new List <string>();
     this.valueList = new List <ExcelRow>();
     foreach (var key in data.Keys)
     {
         ExcelRow row = data[key];
         this.idList.Add(key);
         this.valueList.Add(row);
     }
 }
Beispiel #3
0
        private static void WriteToAsset(ISheet sheet, string fileName, string outputPath)
        {
            if (!CheckSheetIsValid(sheet, fileName, ExcelConst.Start_Data_Row_Index))
            {
                return;
            }
            try
            {
                var rowEnumerator = sheet.GetRowEnumerator();
                var sheetColLen   = ExcelUtil.GetSheetColLen(sheet);
                var headerNames   = ExcelUtil.GetSheetHeaderNames(sheet, ExcelConst.Header_Name_Row_Index);
                //      if (headerNames.Count > sheetColLen)
                //      {
                //        Debug.LogErrorFormat("表格: [{0}] 名字那行的数据长度不对,names.Count:{1},   colLength:{2}", sheet.SheetName,
                //          headerNames.Count, sheetColLen);
                //      }
                //      else
                {
                    var sheetTypes = ExcelUtil.GetSheetHeaderTypes(sheet, ExcelConst.Header_Name_Type_Row_Index);
                    //        if (sheetTypes.Count > sheetColLen)
                    //        {
                    //          Debug.LogErrorFormat("表格: [{0}] 类型那行的数据长度不对", sheet.SheetName);
                    //        }
                    //        else
                    {
                        var excelDatabase = AssetDatabase.LoadAssetAtPath <ExcelDatabase>(outputPath);
                        if (excelDatabase == null)
                        {
                            excelDatabase = ScriptableObjectUtil.CreateAsset <ExcelDatabase>(outputPath);
                        }

                        var linkedDictionary = new LinkedDictionary <string, ExcelRow>();
                        excelDatabase.headerList.Clear();
                        var headers        = excelDatabase.headerList;
                        int curIgnoreCount = 0;
                        for (var i = 0; i < sheetColLen; i++)
                        {
                            //            LogCat.LogError(headerNames[i] + "  " + sheetTypes[i]);
                            if (!ExcelUtil.IsColumnValid(sheet, i))
                            {
                                curIgnoreCount++;
                                continue;
                            }
                            else
                            {
                                headers.Add(new ExcelHeader
                                {
                                    name = headerNames[i - curIgnoreCount],
                                    type = sheetTypes[i - curIgnoreCount]
                                });
                            }
                        }

                        List <string> removeList = new List <string>();                       //多语言表处理
                        //预先处理不符合的cell类型
                        while (rowEnumerator.MoveNext())
                        {
                            var row = rowEnumerator.Current as IRow;

                            if (row.RowNum >= ExcelConst.Start_Data_Row_Index)
                            {
                                if (row.LastCellNum == -1)
                                {
                                    continue;
                                }
                                bool   isHasLang      = false;                          //多语言表处理
                                var    excelValueList = new ExcelRow();
                                string id             = "";
                                curIgnoreCount = 0;
                                for (var j = 0; j < sheetColLen; j++)
                                {
                                    if (!ExcelUtil.IsColumnValid(sheet, j))
                                    {
                                        curIgnoreCount++;
                                        continue;
                                    }

                                    var errorTips = string.Format("表格: [{0}] 第{1}行,第{2}列数据格式不对!", row.RowNum + 1,
                                                                  sheet.SheetName, j + 1);
                                    var cell        = j < (int)row.LastCellNum ? row.GetCell(j) : null;
                                    var sCellValue  = cell == null ? "" : ExcelUtil.GetCellValue(cell);
                                    var excelHeader = headers[j - curIgnoreCount];
                                    var cellType    = excelHeader.type;
                                    var intValue    = 0;
                                    switch (cellType)
                                    {
                                    case ExcelDataType.INT:
                                        if (!string.IsNullOrEmpty(sCellValue) &&
                                            !int.TryParse(sCellValue, out intValue))
                                        {
                                            Debug.LogErrorFormat("表格: [{0}] 第{1}行,第{2}列数据格式不对!", row.RowNum + 1,
                                                                 sheet.SheetName, j + 1);
                                        }
                                        break;

                                    case ExcelDataType.FLOAT:
                                        var floatValue = 0f;
                                        if (!string.IsNullOrEmpty(sCellValue) &&
                                            !float.TryParse(sCellValue, out floatValue))
                                        {
                                            Debug.LogWarning(errorTips);
                                        }
                                        break;

                                    case ExcelDataType.VECTOR3:
                                        var sVector3 = sCellValue.Split(',');
                                        if (sVector3 == null || sVector3.Length != 3)
                                        {
                                            Debug.LogWarning(errorTips);
                                        }
                                        foreach (var v in sVector3)
                                        {
                                            var num5 = 0f;
                                            if (!float.TryParse(v, out num5))
                                            {
                                                Debug.LogWarning(errorTips);
                                            }
                                        }

                                        break;

                                    case ExcelDataType.BOOLEAN:
                                        var boolValue = false;
                                        if (!string.IsNullOrEmpty(sCellValue) &&
                                            !bool.TryParse(sCellValue, out boolValue))
                                        {
                                            Debug.LogWarning(errorTips);
                                        }
                                        break;
                                    }

                                    if (excelHeader.name.ToLower().Equals(ExcelConst.Id_Text))
                                    {
                                        id = sCellValue;
                                    }
                                    else
                                    {
                                        //多语言表处理
                                        if (fileName.Contains("D 多语言表-Lang.xlsx"))
                                        {
                                            if (!sCellValue.IsNullOrWhiteSpace())
                                            {
                                                isHasLang = true;
                                            }
                                        }
                                    }

                                    var excelValue = new ExcelValue();
                                    excelValue.value = sCellValue;
                                    excelValueList.valueList.Add(excelValue);
                                }

                                //多语言表处理,对没有翻译的key不用写asset中,以减少数据
                                if (fileName.Contains("D 多语言表-Lang.xlsx"))
                                {
                                    if (isHasLang == false)
                                    {
                                        removeList.Add(id);
                                    }
                                }

                                if (excelValueList.valueList.Count > 0)
                                {
                                    linkedDictionary[id] = excelValueList;
                                }
                            }
                        }

                        foreach (var toRemoveId in removeList)
                        {
                            linkedDictionary.Remove(toRemoveId);
                        }
                        excelDatabase.SetAssetData(linkedDictionary);
                        EditorUtility.SetDirty(excelDatabase);
                        AssetDatabase.SaveAssets();
                        AssetDatabase.Refresh();
                        Debug.LogFormat("WriteTable: {0} count:{1} to {2}", fileName, linkedDictionary.Count,
                                        outputPath);
                    }
                }
            }
            catch (Exception e)
            {
                Debug.LogException(e);
            }
        }