ExcelReader.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System.IO;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using Excel;
  5. using System.Data;
  6. using GFGEditor;
  7. namespace GFGEditor
  8. {
  9. public class ExcelReader
  10. {
  11. public delegate void RowCollectionHandler(DataRowCollection rowCollection, string configName, string configArrayName);
  12. public static void ReadExcel(RowCollectionHandler rowCollectionHandler)
  13. {
  14. string[] files = Directory.GetFiles(ExcelConfig.excelsFolderPath);
  15. int totalCount = files.Length;
  16. for (int i = 0; i < totalCount; i++)
  17. {
  18. string filePath = files[i];
  19. string fileName = Path.GetFileNameWithoutExtension(filePath);
  20. if(!fileName.Contains("~"))
  21. {
  22. FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
  23. IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
  24. DataSet result = excelReader.AsDataSet();
  25. HandleTableCollection(result.Tables, rowCollectionHandler);
  26. }
  27. }
  28. }
  29. private static void HandleTableCollection(DataTableCollection collection, RowCollectionHandler rowCollectionHandler)
  30. {
  31. for (int i = 0; i < collection.Count; i++)
  32. {
  33. DataTable table = collection[i];
  34. HandleTable(table, rowCollectionHandler);
  35. }
  36. }
  37. private static void HandleTable(DataTable table, RowCollectionHandler rowCollectionHandler)
  38. {
  39. string[] names = table.TableName.Split('_');
  40. if (names.Length < 2)
  41. {
  42. //未正确命名的表格
  43. return;
  44. }
  45. if (table.TableName.Contains("#"))
  46. {
  47. //被注释的表格
  48. return;
  49. }
  50. string configItemName = names[1];
  51. string managerTag = "";
  52. if (names.Length > 2)
  53. {
  54. managerTag = names[2];
  55. }
  56. //文件名及管理器名
  57. string configManagerName = string.Format(ExcelConfig.CONFIG_ARRAY_TEMPLATE, configItemName, managerTag);
  58. //获得表数据
  59. DataRowCollection rowCollection = table.Rows;
  60. rowCollectionHandler(rowCollection, configItemName, configManagerName);
  61. }
  62. }
  63. }