Browse Source

更新EPPlus导表库

guodong 3 years ago
parent
commit
1bb4b45f24

+ 21 - 18
GameClient/Assets/Editor/Excel/CodeBuilder.cs

@@ -8,6 +8,7 @@ using UnityEngine;
 using GFGGame;
 using System.Text.RegularExpressions;
 using static Google.Protobuf.Reflection.GeneratedCodeInfo.Types;
+using OfficeOpenXml;
 
 namespace GFGEditor
 {
@@ -25,8 +26,9 @@ namespace GFGEditor
         private static Dictionary<string, string> _idDic = new Dictionary<string, string>();
         private static Dictionary<string, string> _itemTypeDicByName = new Dictionary<string, string>();
 
-        public static void GenerateCode(DataRowCollection rowCollection, string configName, string configArrayName)
+        public static void GenerateCode(ExcelWorksheet worksheet, string configName, string configArrayName)
         {
+            ET.Log.Debug($"configName {configName}");
             _declarationBuilder.Clear();
             _assignmentBuilder.Clear();
             _parseBuilder.Clear();
@@ -41,19 +43,20 @@ namespace GFGEditor
             List<string> groupTypes = new List<string>();
             bool needAll = false;
             bool groupOnly = false;
-            int len = rowCollection[1].ItemArray.Length;
+            int len = worksheet.Dimension.End.Column;
             if (len <= 0)
             {
                 return;
             }
-            for (int i = 0; i < len; ++i)
+            for (int i = 1; i <= len; ++i)
             {
-                string annotation = rowCollection[0][i].ToString();
-                string type = rowCollection[1][i].ToString();
-                string nameWhole = rowCollection[2][i].ToString();
+                ET.Log.Debug($"GenerateCode for i {i}");
+                string annotation = worksheet.Cells[1, i].Text.Trim();
+                string type = worksheet.Cells[2, i].Text.Trim();
+                string nameWhole = worksheet.Cells[3, i].Text.Trim();
                 string[] nameInfos = nameWhole.Split('#');
                 string name = nameInfos[0];
-                if (i == 0 || nameWhole.Contains("#k"))
+                if (i == 1 || nameWhole.Contains("#k"))
                 {
                     keyNames.Add(name);
                     keyTypes.Add(type);
@@ -79,7 +82,7 @@ namespace GFGEditor
             }
             //创建sqlite表
             SQLiteHelper.Instance.CreateTable(configArrayName, _names.ToArray(), _types.ToArray());
-            AddDataToSql(rowCollection, configName, configArrayName);
+            AddDataToSql(worksheet, configName, configArrayName);
 
             //生成管理类代码
             string configArrayStr = CodeTemplateFactory.ConfigArrayTemplate;
@@ -241,19 +244,18 @@ namespace GFGEditor
         }
 
 
-        private static void AddDataToSql(DataRowCollection rowCollection, string configName, string configArrayName)
+        private static void AddDataToSql(ExcelWorksheet worksheet, string configName, string configArrayName)
         {
-            int rowNum = rowCollection.Count;
-            for (int i = 4; i < rowNum; i++)
+            int rowNum = worksheet.Dimension.End.Row;
+            for (int i = 5; i <= rowNum; i++)
             {
-                DataRow dataRow = rowCollection[i];
-                string key = dataRow[0].ToString();
+                string key = worksheet.Cells[i, 1].Text.Trim();
                 if (key.Length == 0)
                 {
                     continue;
                 }
                 //解析每行的数据
-                WriteRowDataToSqlite(dataRow, configArrayName);
+                WriteRowDataToSqlite(worksheet.Cells, configArrayName, i);
                 if (_idDic.ContainsKey(key))
                 {
                     _hasSameIds = true;
@@ -265,14 +267,14 @@ namespace GFGEditor
             }
         }
 
-        private static void WriteRowDataToSqlite(DataRow row, string configArrayName)
+        private static void WriteRowDataToSqlite(ExcelRange excelRange, string configArrayName, int row)
         {
             List<string> values = new List<string>();
-            var keyValue = row[0].ToString();
+            var keyValue = excelRange[row, 1].Text.Trim();
             foreach (var i in _indexs)
             {
-                var fieldName = _names[i];
-                var value = row[i].ToString();
+                var fieldName = _names[i-1];
+                var value = excelRange[row, i].Text.Trim();
                 if (configArrayName == nameof(ItemTypeCfgArray))
                 {
                     CacheItemTypeByName(keyValue, fieldName, value);
@@ -299,6 +301,7 @@ namespace GFGEditor
         private static void HandleItemCfgField(string idStr, string fieldName, ref string value)
         {
             fieldName = fieldName.Substring(1);
+            ET.Log.Debug($"idStr {idStr} fieldName {fieldName} value {value}");
             int id = int.Parse(idStr);
             int itemType = (int)Mathf.Floor(id / GameConst.MAX_COUNT_EVERY_TYPE_ITEM);
             if (fieldName == "itemType")

BIN
GameClient/Assets/Editor/Excel/DLL/EPPlus.dll


BIN
GameClient/Assets/Editor/Excel/DLL/EPPlusFree.dll


+ 1 - 1
GameClient/Assets/Editor/Excel/DLL/EPPlus.dll.meta → GameClient/Assets/Editor/Excel/DLL/EPPlusFree.dll.meta

@@ -1,5 +1,5 @@
 fileFormatVersion: 2
-guid: 3e820c544e1b1e04099e758fbf8b7a6d
+guid: 420801e38bdef0c44bf5f7352caa3520
 PluginImporter:
   externalObjects: {}
   serializedVersion: 2

BIN
GameClient/Assets/Editor/Excel/DLL/Excel.dll


+ 0 - 33
GameClient/Assets/Editor/Excel/DLL/Excel.dll.meta

@@ -1,33 +0,0 @@
-fileFormatVersion: 2
-guid: 9e4fa1d3b4a1dbf43a8079d55ac551ee
-PluginImporter:
-  externalObjects: {}
-  serializedVersion: 2
-  iconMap: {}
-  executionOrder: {}
-  defineConstraints: []
-  isPreloaded: 0
-  isOverridable: 0
-  isExplicitlyReferenced: 0
-  validateReferences: 1
-  platformData:
-  - first:
-      Any: 
-    second:
-      enabled: 0
-      settings: {}
-  - first:
-      Editor: Editor
-    second:
-      enabled: 1
-      settings:
-        DefaultValueInitialized: true
-  - first:
-      Windows Store Apps: WindowsStoreApps
-    second:
-      enabled: 0
-      settings:
-        CPU: AnyCPU
-  userData: 
-  assetBundleName: 
-  assetBundleVariant: 

+ 18 - 16
GameClient/Assets/Editor/Excel/ExcelReader.cs

@@ -1,15 +1,15 @@
 using System.IO;
 using UnityEditor;
 using UnityEngine;
-using Excel;
 using System.Data;
 using GFGEditor;
+using OfficeOpenXml;
 
 namespace GFGEditor
 {
     public class ExcelReader
     {
-        public delegate void RowCollectionHandler(DataRowCollection rowCollection, string configName, string configArrayName);
+        public delegate void RowCollectionHandler(ExcelWorksheet rowCollection, string configName, string configArrayName);
 
         public static void ReadExcel(RowCollectionHandler rowCollectionHandler)
         {
@@ -21,32 +21,35 @@ namespace GFGEditor
                 string fileName = Path.GetFileNameWithoutExtension(filePath);
                 if(!fileName.Contains("~"))
                 {
-                    FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
-                    IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
-                    DataSet result = excelReader.AsDataSet();
-                    HandleTableCollection(result.Tables, rowCollectionHandler);
+                    ET.Log.Debug($"fileName {fileName}");
+                    Stream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
+                    ExcelPackage excelPackage = new ExcelPackage(stream);
+                    //IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
+                    //DataSet result = excelReader.AsDataSet();
+                    
+                    HandleTableCollection(excelPackage.Workbook.Worksheets, rowCollectionHandler);
                 }
             }
         }
 
-        private static void HandleTableCollection(DataTableCollection collection, RowCollectionHandler rowCollectionHandler)
+        private static void HandleTableCollection(ExcelWorksheets Worksheets, RowCollectionHandler rowCollectionHandler)
         {
-            for (int i = 0; i < collection.Count; i++)
+            for (int i = 1; i <= Worksheets.Count; i++)
             {
-                DataTable table = collection[i];
-                HandleTable(table, rowCollectionHandler);
+                ExcelWorksheet worksheet = Worksheets[i];
+                HandleTable(worksheet, rowCollectionHandler);
             }
         }
 
-        private static void HandleTable(DataTable table, RowCollectionHandler rowCollectionHandler)
+        private static void HandleTable(ExcelWorksheet worksheet, RowCollectionHandler rowCollectionHandler)
         {
-            string[] names = table.TableName.Split('_');
+            string[] names = worksheet.Name.Split('_');
             if (names.Length < 2)
             {
                 //未正确命名的表格
                 return;
             }
-            if (table.TableName.Contains("#"))
+            if (worksheet.Name.Contains("#"))
             {
                 //被注释的表格
                 return;
@@ -59,10 +62,9 @@ namespace GFGEditor
             }
             //文件名及管理器名
             string configManagerName = string.Format(ExcelConfig.CONFIG_ARRAY_TEMPLATE, configItemName, managerTag);
-            //获得表数据
-            DataRowCollection rowCollection = table.Rows;
+            
 
-            rowCollectionHandler(rowCollection, configItemName, configManagerName);
+            rowCollectionHandler(worksheet, configItemName, configManagerName);
         }
 
     }

+ 1 - 1
GameClient/Assets/Editor/ToolsMenu.cs

@@ -123,7 +123,7 @@ namespace GFGEditor
             ET.Options.Instance = new ET.Options();
             EditorUtility.DisplayProgressBar("进度", "正在导入表格", 1);
             SqliteController.Instance.dirPath = Path.Combine(Environment.CurrentDirectory, ResPathUtil.CONFIG_DIR_PATH);
-            SqliteController.Instance.Init(true, null);
+            SqliteController.Instance.Init(false, null);
             SQLiteHelper.Instance.OpenConnection();
             //try
             //{