ExcelReader.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. using System.IO;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using System.Data;
  5. using GFGEditor;
  6. using OfficeOpenXml;
  7. using System;
  8. namespace GFGEditor
  9. {
  10. public class ExcelReader
  11. {
  12. public delegate void RowCollectionHandler(ExcelWorksheet rowCollection, string configName, string configArrayName);
  13. public static void ReadExcel(RowCollectionHandler rowCollectionHandler)
  14. {
  15. string[] files = Directory.GetFiles(ExcelConfig.excelsCacheFolderPath);
  16. int totalCount = files.Length;
  17. string strCfgArrayDispose = "";
  18. string strCfgArrayInit = "";
  19. for (int i = 0; i < totalCount; i++)
  20. {
  21. string filePath = files[i];
  22. string fileName = Path.GetFileNameWithoutExtension(filePath);
  23. if (!fileName.Contains("~"))
  24. {
  25. ET.Log.Debug($"fileName {fileName}");
  26. Stream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
  27. ExcelPackage excelPackage = new ExcelPackage(stream);
  28. //IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
  29. //DataSet result = excelReader.AsDataSet();
  30. HandleTableCollection(excelPackage.Workbook.Worksheets, rowCollectionHandler, out string dispose, out string init);
  31. strCfgArrayDispose = strCfgArrayDispose + dispose;
  32. strCfgArrayInit = strCfgArrayInit + init;
  33. }
  34. }
  35. string functionDispose = CodeTemplateFactory.FunctionDisposeTemplate;
  36. functionDispose = functionDispose.Replace("{StrCfgArrayDispose}", strCfgArrayDispose);
  37. string disposeAllCfgsCache = CodeTemplateFactory.DisposeAllCfgsCacheTemplate;
  38. disposeAllCfgsCache = disposeAllCfgsCache.Replace("{FunctionDispose}", functionDispose);
  39. using (StreamWriter sw = new StreamWriter(ExcelConfig.handleAllCfgsCache + "DisposeAllCfgsCache.cs"))
  40. {
  41. sw.Write(disposeAllCfgsCache);
  42. }
  43. string functionInit = CodeTemplateFactory.FunctionInitTemplate;
  44. functionInit = functionInit.Replace("{StrCfgArrayInit}", strCfgArrayInit);
  45. string initAllCfgsCache = CodeTemplateFactory.InitAllCfgsCacheTemplate;
  46. initAllCfgsCache = initAllCfgsCache.Replace("{FunctionInit}", functionInit);
  47. using (StreamWriter sw = new StreamWriter(ExcelConfig.handleAllCfgsCache + "InitAllCfgsCache.cs"))
  48. {
  49. sw.Write(initAllCfgsCache);
  50. }
  51. }
  52. private static void HandleTableCollection(ExcelWorksheets Worksheets, RowCollectionHandler rowCollectionHandler, out string strDispose, out string strInit)
  53. {
  54. strDispose = "";
  55. strInit = "";
  56. for (int i = 1; i <= Worksheets.Count; i++)
  57. {
  58. ExcelWorksheet worksheet = Worksheets[i];
  59. string configArrayName = HandleTable(worksheet, rowCollectionHandler);
  60. if (string.IsNullOrEmpty(configArrayName)) continue;
  61. string dispose = CodeTemplateFactory.StrCfgArrayDisposeTemplate.Replace("{configArrayName}", configArrayName);
  62. strDispose = strDispose + "\n" + dispose;
  63. string init = CodeTemplateFactory.StrCfgArrayInitTemplate.Replace("{configArrayName}", configArrayName);
  64. strInit = strInit + "\n" + init;
  65. }
  66. }
  67. private static string HandleTable(ExcelWorksheet worksheet, RowCollectionHandler rowCollectionHandler)
  68. {
  69. string[] names = worksheet.Name.Split('_');
  70. if (names.Length < 2)
  71. {
  72. //未正确命名的表格
  73. return "";
  74. }
  75. if (worksheet.Name.Contains("#"))
  76. {
  77. //被注释的表格
  78. return "";
  79. }
  80. string configItemName = names[1];
  81. string managerTag = "";
  82. if (names.Length > 2)
  83. {
  84. managerTag = names[2];
  85. }
  86. //文件名及管理器名
  87. string configManagerName = string.Format(ExcelConfig.CONFIG_ARRAY_TEMPLATE, configItemName, managerTag);
  88. rowCollectionHandler(worksheet, configItemName, configManagerName);
  89. return configManagerName;
  90. }
  91. public static void WriteExcle()
  92. {
  93. DeleteExcle();
  94. string[] files = Directory.GetFiles(ExcelConfig.excelsFolderPath);
  95. int totalCount = files.Length;
  96. for (int i = 0; i < totalCount; i++)
  97. {
  98. string filePath = files[i];
  99. string fileName = Path.GetFileNameWithoutExtension(filePath);
  100. if (!fileName.Contains("~") && !fileName.Contains("0000自动生表工具") && !fileName.Contains("zzzzGFG数值"))
  101. {
  102. string newPath = ExcelConfig.excelsCacheFolderPath + fileName + ".xlsx";
  103. Stream newStream = new FileStream(newPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
  104. ExcelPackage newExcelPackage = new ExcelPackage(newStream);
  105. ExcelWorksheets newWorksheets = newExcelPackage.Workbook.Worksheets;
  106. Stream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
  107. ExcelPackage excelPackage = new ExcelPackage(stream);
  108. ExcelWorksheets Worksheets = excelPackage.Workbook.Worksheets;
  109. for (int j = 1; j <= Worksheets.Count; j++)
  110. {
  111. ExcelWorksheet worksheet = Worksheets[j];
  112. string[] names = worksheet.Name.Split('_');
  113. if (names.Length < 2) continue;
  114. ExcelWorksheet newWorksheet = null;
  115. for (int k = 1; k <= newWorksheets.Count; k++)
  116. {
  117. string[] newNames = newWorksheets[k].Name.Split('_');
  118. if (names.Length < 2 || names.Length != newNames.Length) continue;
  119. for (int h = 1; h < names.Length; h++)
  120. {
  121. if (newNames[h] == names[h])
  122. {
  123. newWorksheet = newWorksheets[k];
  124. }
  125. else
  126. {
  127. newWorksheet = null;
  128. break;
  129. }
  130. }
  131. }
  132. if (newWorksheet == null)
  133. {
  134. newWorksheets.Add(worksheet.Name);
  135. newWorksheet = newWorksheets[newWorksheets.Count];
  136. }
  137. WriteTitle(newWorksheet, worksheet);
  138. // Debug.Log(worksheet.Name + "写入完成");
  139. }
  140. newExcelPackage.Save();
  141. excelPackage.Save();
  142. newStream.Close();
  143. stream.Close();
  144. newStream.Dispose();
  145. stream.Dispose();
  146. Debug.Log(fileName + "写入完成");
  147. }
  148. }
  149. }
  150. private static void WriteTitle(ExcelWorksheet newWorksheet, ExcelWorksheet worksheet)
  151. {
  152. int newColumnNum = newWorksheet.Dimension == null ? 0 : newWorksheet.Dimension.End.Column;
  153. int columnNum = worksheet.Dimension.End.Column;
  154. for (int i = 1; i <= columnNum; i++)
  155. {
  156. string name = worksheet.Cells[3, i].Text.Trim();
  157. string type = worksheet.Cells[2, i].Text.Trim();
  158. if (name.Length == 0)
  159. {
  160. continue;
  161. }
  162. bool needWriteColumn = true;
  163. for (int j = 1; j <= newColumnNum; j++)
  164. {
  165. string newName = newWorksheet.Cells[3, j].Text.Trim();
  166. string newType = newWorksheet.Cells[2, j].Text.Trim();
  167. if (name == newName && type == newType)
  168. {
  169. needWriteColumn = false;
  170. break;
  171. }
  172. }
  173. if (needWriteColumn)
  174. {
  175. newColumnNum = newWorksheet.Dimension == null ? 0 : newWorksheet.Dimension.End.Column;
  176. newWorksheet.InsertColumn(newColumnNum + 1, 1);
  177. for (int k = 1; k <= 4; k++)
  178. {
  179. newWorksheet.Cells[k, newColumnNum + 1].Value = worksheet.Cells[k, i].Value;
  180. }
  181. }
  182. }
  183. WriteCell(newWorksheet, worksheet);
  184. }
  185. private static void WriteCell(ExcelWorksheet newWorksheet, ExcelWorksheet worksheet)
  186. {
  187. int newRowNum = newWorksheet.Dimension == null ? 0 : newWorksheet.Dimension.End.Row;
  188. newWorksheet.InsertRow(newRowNum + 1, 2);
  189. int newColumnNum = newWorksheet.Dimension == null ? 0 : newWorksheet.Dimension.End.Column;
  190. int rowNum = worksheet.Dimension.End.Row;
  191. int columnNum = worksheet.Dimension.End.Column;
  192. for (int i = 5; i <= rowNum; i++)
  193. {
  194. newRowNum = newWorksheet.Dimension == null ? 0 : newWorksheet.Dimension.End.Row;
  195. newWorksheet.InsertRow(newRowNum + 1, 1);
  196. int column = 0;
  197. for (int j = 1; j <= columnNum; j++)
  198. {
  199. string title = worksheet.Cells[3, j].Text.Trim();
  200. if (string.IsNullOrEmpty(title)) continue;
  201. for (int k = 1; k <= newColumnNum; k++)
  202. {
  203. string newTitle = newWorksheet.Cells[3, k].Text.Trim();
  204. if (newTitle == title)
  205. {
  206. column = k;
  207. break;
  208. }
  209. }
  210. newWorksheet.Cells[newRowNum + 1, column].Value = worksheet.Cells[i, j].Value;
  211. }
  212. }
  213. newRowNum = newWorksheet.Dimension == null ? 0 : newWorksheet.Dimension.End.Row;
  214. newWorksheet.InsertRow(newRowNum + 1, 2);
  215. }
  216. private static void DeleteExcle()
  217. {
  218. string path = ExcelConfig.excelsCacheFolderPath;
  219. if (Directory.Exists(path) == false)
  220. {
  221. Directory.CreateDirectory(path);
  222. }
  223. DirectoryInfo dir = new DirectoryInfo(path);
  224. // FileInfo[] files = dir.GetFiles();
  225. string[] files = Directory.GetFiles(path);
  226. try
  227. {
  228. foreach (var item in files)
  229. {
  230. File.Delete(item);
  231. }
  232. return;
  233. }
  234. catch (Exception)
  235. {
  236. ET.Log.Debug("Delete Failed!");
  237. return;
  238. }
  239. }
  240. }
  241. }