ExcelHandler.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using UnityEngine;
  4. public static class ExcelHandler
  5. {
  6. /// <summary>
  7. /// 读取 Excel 中第一列
  8. /// </summary>
  9. public static List<string> ReadFirstColumn(string deviceName)
  10. {
  11. List<string> list = new List<string>();
  12. string path = PathHelper.CombineFilePath(PathHelper.m_ReadOnlyPath, deviceName) + GlobalConfig.excelSuffix;
  13. if (!File.Exists(path))
  14. {
  15. Debug.LogError("Excel 文件不存在:" + path);
  16. return list;
  17. }
  18. List<MoveData> rows = ExcelHelper.ReadInfoFromExcel<MoveData>(path, 1);
  19. if (rows == null || rows.Count == 0) return list;
  20. HashSet<string> seen = new HashSet<string>();
  21. foreach (var r in rows)
  22. {
  23. if (string.IsNullOrEmpty(r.moveName)) continue;
  24. if (seen.Add(r.moveName)) list.Add(r.moveName);
  25. }
  26. return list;
  27. }
  28. /// <summary>
  29. /// 写回 Excel 第一列(名字顺序)
  30. /// </summary>
  31. public static void WriteFirstColumn(string deviceName, List<string> names)
  32. {
  33. string path = PathHelper.CombineFilePath(PathHelper.m_ReadOnlyPath, deviceName) + GlobalConfig.excelSuffix;
  34. List<MoveData> list = new List<MoveData>();
  35. foreach (var n in names) list.Add(new MoveData { moveName = n });
  36. ExcelHelper.WriteInfoToExcel(path, list);
  37. Debug.Log($"已写回 Excel 顺序:{path}({names.Count} 条)");
  38. }
  39. }