ExcellReadWrite.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using NPOI.SS.UserModel;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using UnityEditor;
  6. using UnityEngine;
  7. using static MedicineAllConfig;
  8. public static class MedicineAllConfigConfiguration
  9. {
  10. public static string ExcellPath = Application.dataPath + "/Config/MedicineAllConfig.xlsx";
  11. public static string excellDataPath = "Assets/Resources/Config/Excel/MedicineAllConfig.asset";
  12. public static int id = 0;
  13. public static int name = 1;
  14. public static int pinType = 2;
  15. public static int characteristic = 3;
  16. public static int growIntroduce = 4;
  17. public static int morphology = 5;
  18. public static void UpdateConfig()
  19. {
  20. MedicineAllConfig config = ScriptableObject.CreateInstance<MedicineAllConfig>();
  21. var info = ExcelTool.ReadExcel(ExcellPath);
  22. try
  23. {
  24. if (info.Item1 == null)
  25. {
  26. File.Delete(info.Item2);
  27. Debug.LogError("读表失败!");
  28. return;
  29. }
  30. List<MedicineEle> items = new List<MedicineEle>();
  31. MedicineEle medele;
  32. // 遍历工作表的行
  33. for (int rowIndex = 2; rowIndex <= info.Item1.LastRowNum; rowIndex++)
  34. {
  35. IRow row = info.Item1.GetRow(rowIndex);
  36. if (row != null)
  37. {
  38. medele = new MedicineEle();
  39. if (string.IsNullOrEmpty(row.GetCell(id).GetCellContent()))
  40. continue;
  41. //// 遍历行中的单元格
  42. medele.id = int.Parse(row.GetCell(id).GetCellContent());
  43. medele.name = string.IsNullOrEmpty(row.GetCell(name).ToString()) ? "" : row.GetCell(name).ToString();
  44. medele.pinType = string.IsNullOrEmpty(row.GetCell(pinType).ToString()) ? char.MinValue : char.Parse(row.GetCell(pinType).ToString());
  45. medele.characteristic = string.IsNullOrEmpty(row.GetCell(characteristic).ToString()) ? "" : row.GetCell(characteristic).ToString();
  46. medele.growIntroduce = string.IsNullOrEmpty(row.GetCell(growIntroduce).ToString()) ? "" : row.GetCell(growIntroduce).ToString();
  47. medele.morphology = string.IsNullOrEmpty(row.GetCell(morphology).ToString()) ? "" : row.GetCell(morphology).ToString();
  48. items.Add(medele);
  49. }
  50. }
  51. config.list = items;
  52. File.Delete(info.Item2);
  53. config.SaveConfig(excellDataPath);
  54. }
  55. catch (global::System.Exception e)
  56. {
  57. File.Delete(info.Item2);
  58. Debug.LogException(e);
  59. }
  60. }
  61. }
  62. public static class ExcellReadWrite
  63. {
  64. [MenuItem("Tools/更新表格")]
  65. static void UpdateConfig()
  66. {
  67. MedicineAllConfigConfiguration.UpdateConfig();
  68. AssetDatabase.SaveAssets();
  69. AssetDatabase.Refresh();
  70. Debug.Log("更新完成!");
  71. }
  72. public static void SaveConfig(this ScriptableObject config, string excellAssetPath)
  73. {
  74. if (string.IsNullOrEmpty(AssetDatabase.GetAssetPath(config)))
  75. AssetDatabase.CreateAsset(config, excellAssetPath);
  76. EditorUtility.SetDirty(config);
  77. }
  78. // 获取单元格的文本内容
  79. public static string GetCellContent(this ICell cell)
  80. {
  81. if (cell != null)
  82. {
  83. switch (cell.CellType)
  84. {
  85. case CellType.String:
  86. return cell.StringCellValue;
  87. case CellType.Numeric:
  88. if (DateUtil.IsCellDateFormatted(cell))
  89. {
  90. return cell.DateCellValue.ToString();
  91. }
  92. else
  93. {
  94. return cell.NumericCellValue.ToString();
  95. }
  96. case CellType.Boolean:
  97. return cell.BooleanCellValue.ToString();
  98. case CellType.Formula:
  99. return cell.CellFormula;
  100. case CellType.Blank:
  101. return string.Empty;
  102. default:
  103. return cell.ToString();
  104. }
  105. }
  106. return string.Empty;
  107. }
  108. }