using System.IO; using UnityEditor; using UnityEngine; using Excel; using System.Data; using GFGEditor; namespace GFGEditor { public class ExcelReader { public delegate void RowCollectionHandler(DataRowCollection rowCollection, string configName, string configArrayName); public static void ReadExcel(RowCollectionHandler rowCollectionHandler) { string[] files = Directory.GetFiles(ExcelConfig.excelsFolderPath); int totalCount = files.Length; for (int i = 0; i < totalCount; i++) { string filePath = files[i]; 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); } } } private static void HandleTableCollection(DataTableCollection collection, RowCollectionHandler rowCollectionHandler) { for (int i = 0; i < collection.Count; i++) { DataTable table = collection[i]; HandleTable(table, rowCollectionHandler); } } private static void HandleTable(DataTable table, RowCollectionHandler rowCollectionHandler) { string[] names = table.TableName.Split('_'); if (names.Length < 2) { //未正确命名的表格 return; } if (table.TableName.Contains("#")) { //被注释的表格 return; } string configItemName = names[1]; string managerTag = ""; if (names.Length > 2) { managerTag = names[2]; } //文件名及管理器名 string configManagerName = string.Format(ExcelConfig.CONFIG_ARRAY_TEMPLATE, configItemName, managerTag); //获得表数据 DataRowCollection rowCollection = table.Rows; rowCollectionHandler(rowCollection, configItemName, configManagerName); } } }