123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- 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);
- }
- }
- }
|