ExcelReader.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System.IO;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using System.Data;
  5. using GFGEditor;
  6. using OfficeOpenXml;
  7. namespace GFGEditor
  8. {
  9. public class ExcelReader
  10. {
  11. public delegate void RowCollectionHandler(ExcelWorksheet 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. string strCfgArrayDispose = "";
  17. for (int i = 0; i < totalCount; i++)
  18. {
  19. string filePath = files[i];
  20. string fileName = Path.GetFileNameWithoutExtension(filePath);
  21. if (!fileName.Contains("~"))
  22. {
  23. ET.Log.Debug($"fileName {fileName}");
  24. Stream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
  25. ExcelPackage excelPackage = new ExcelPackage(stream);
  26. //IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
  27. //DataSet result = excelReader.AsDataSet();
  28. HandleTableCollection(excelPackage.Workbook.Worksheets, rowCollectionHandler, out string dispose);
  29. strCfgArrayDispose = strCfgArrayDispose + dispose;
  30. }
  31. }
  32. string functionDispose = CodeTemplateFactory.FunctionDisposeTemplate;
  33. functionDispose = functionDispose.Replace("{StrCfgArrayDispose}", strCfgArrayDispose);
  34. string disposeAllCfgsCache = CodeTemplateFactory.DisposeAllCfgsCacheTemplate;
  35. disposeAllCfgsCache = disposeAllCfgsCache.Replace("{FunctionDispose}", functionDispose);
  36. //创建管理类代码
  37. using (StreamWriter sw = new StreamWriter(ExcelConfig.configDataDisposePath + "DisposeAllCfgsCache.cs"))
  38. {
  39. sw.Write(disposeAllCfgsCache);
  40. }
  41. }
  42. private static void HandleTableCollection(ExcelWorksheets Worksheets, RowCollectionHandler rowCollectionHandler, out string strCfgArrayDispose)
  43. {
  44. strCfgArrayDispose = "";
  45. for (int i = 1; i <= Worksheets.Count; i++)
  46. {
  47. ExcelWorksheet worksheet = Worksheets[i];
  48. string configArrayName = HandleTable(worksheet, rowCollectionHandler);
  49. if (string.IsNullOrEmpty(configArrayName)) continue;
  50. string dispose = CodeTemplateFactory.StrCfgArrayDisposeTemplate.Replace("{configArrayName}", configArrayName);
  51. strCfgArrayDispose = strCfgArrayDispose + "\n" + dispose;
  52. }
  53. }
  54. private static string HandleTable(ExcelWorksheet worksheet, RowCollectionHandler rowCollectionHandler)
  55. {
  56. string[] names = worksheet.Name.Split('_');
  57. if (names.Length < 2)
  58. {
  59. //未正确命名的表格
  60. return "";
  61. }
  62. if (worksheet.Name.Contains("#"))
  63. {
  64. //被注释的表格
  65. return "";
  66. }
  67. string configItemName = names[1];
  68. string managerTag = "";
  69. if (names.Length > 2)
  70. {
  71. managerTag = names[2];
  72. }
  73. //文件名及管理器名
  74. string configManagerName = string.Format(ExcelConfig.CONFIG_ARRAY_TEMPLATE, configItemName, managerTag);
  75. rowCollectionHandler(worksheet, configItemName, configManagerName);
  76. return configManagerName;
  77. }
  78. }
  79. }