PlcExcelLoad.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Formats.Asn1;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using NPOI.SS.UserModel;
  11. using NPOI.XSSF.UserModel;
  12. namespace TeamAAS_VP.Models.PLC
  13. {
  14. public class PlcExcelLoad
  15. {
  16. /// <summary>
  17. /// 真正的 bool 类型地址结构(无字符串类型)
  18. /// </summary>
  19. public class PLCAddrs
  20. {
  21. public string OpcAddress { get; set; } // PLC地址(变量名)
  22. public string Name { get; set; } // 报警文本
  23. public bool Value { get; set; } // 真实 BOOL 类型值
  24. }
  25. // 默认PLC地址表路径
  26. private string _filePath = Path.Combine(Path.GetDirectoryName(Application.StartupPath), @"Config\PLC地址表.xlsx");
  27. // 所有集合统一使用 PLCAddrs
  28. public List<PLCAddrs> IOInputList = new List<PLCAddrs>();
  29. public List<PLCAddrs> IOOutputList = new List<PLCAddrs>();
  30. public List<PLCAddrs> JoggingList = new List<PLCAddrs>();
  31. public List<PLCAddrs> AlarmMaskList = new List<PLCAddrs>();
  32. public List<PLCAddrs> InputParamList = new List<PLCAddrs>();
  33. public List<PLCAddrs> OutputParamList = new List<PLCAddrs>();
  34. public List<PLCAddrs> AlaemList = new List<PLCAddrs>(); // 报警列表
  35. public List<PLCAddrs> SysStateList = new List<PLCAddrs>(); // 系统状态
  36. public string FilePath
  37. {
  38. get => _filePath;
  39. set => _filePath = value;
  40. }
  41. public PlcExcelLoad(string filePathStr = null)
  42. {
  43. if (!string.IsNullOrEmpty(filePathStr))
  44. _filePath = filePathStr;
  45. }
  46. private DataTable GetCsvData()
  47. {
  48. DataTable dt = new DataTable();
  49. try
  50. {
  51. // 优先用GB2312兼容Excel导出的CSV,乱码时可改为Encoding.UTF8/Encoding.Default
  52. Encoding encoding = Encoding.GetEncoding("GB2312");
  53. string[] lines = File.ReadAllLines(_filePath, encoding);
  54. if (lines.Length == 0) return null;
  55. // 读取表头
  56. string[] headers = lines[0].Split(',');
  57. foreach (string header in headers)
  58. {
  59. dt.Columns.Add(header.Trim());
  60. }
  61. // 读取数据行
  62. for (int i = 1; i < lines.Length; i++)
  63. {
  64. string[] cells = lines[i].Split(',');
  65. DataRow row = dt.NewRow();
  66. for (int j = 0; j < dt.Columns.Count; j++)
  67. {
  68. row[j] = j < cells.Length ? cells[j].Trim() : "";
  69. }
  70. dt.Rows.Add(row);
  71. }
  72. return dt;
  73. }
  74. catch
  75. {
  76. return null;
  77. }
  78. }
  79. /// <summary>
  80. /// 加载Excel数据
  81. /// </summary>
  82. public void LoadData(int sheetIndex)
  83. {
  84. try
  85. {
  86. if (!File.Exists(_filePath)) return;
  87. DataTable dt = null;
  88. string ext = Path.GetExtension(_filePath).ToLower();
  89. dt = GetCsvData();
  90. if (dt == null || dt.Rows.Count == 0) return;
  91. RemoveEmptyRow(dt);
  92. foreach (DataRow row in dt.Rows)
  93. {
  94. PLCAddrs temp = new PLCAddrs();
  95. switch (sheetIndex)
  96. {
  97. case 0: // IO输入
  98. temp.OpcAddress = row[0].ToString().Trim();
  99. temp.Name = row[1].ToString().Trim();
  100. IOInputList.Add(temp);
  101. break;
  102. case 1: // IO输出
  103. temp.OpcAddress = row[0].ToString().Trim();
  104. temp.Name = row[1].ToString().Trim();
  105. IOOutputList.Add(temp);
  106. break;
  107. case 2: // 点动
  108. temp.OpcAddress = row[0].ToString().Trim();
  109. temp.Name = row[1].ToString().Trim();
  110. JoggingList.Add(temp);
  111. break;
  112. case 3: // 参数输入
  113. temp.OpcAddress = row[0].ToString().Trim();
  114. temp.Name = row[1].ToString().Trim();
  115. InputParamList.Add(temp);
  116. break;
  117. case 4: // 参数输出
  118. temp.OpcAddress = row[0].ToString().Trim();
  119. temp.Name = row[1].ToString().Trim();
  120. OutputParamList.Add(temp);
  121. break;
  122. case 5: // 报警屏蔽
  123. temp.OpcAddress = row[0].ToString().Trim();
  124. temp.Name = row[1].ToString().Trim();
  125. AlarmMaskList.Add(temp);
  126. break;
  127. case 6: // 报警信息(A列名称,B列地址)
  128. temp.Name = row[0].ToString().Trim(); // 报警文本
  129. temp.OpcAddress = row[1].ToString().Trim(); // PLC地址
  130. AlaemList.Add(temp);
  131. break;
  132. case 7: // 系统状态
  133. temp.OpcAddress = row[0].ToString().Trim();
  134. temp.Name = row[1].ToString().Trim();
  135. SysStateList.Add(temp);
  136. break;
  137. }
  138. }
  139. }
  140. catch (Exception ex)
  141. {
  142. MessageBox.Show($"加载PLC地址表失败:{ex.Message}");
  143. }
  144. }
  145. // 清除空行
  146. public void RemoveEmptyRow(DataTable dt)
  147. {
  148. List<DataRow> delRows = new List<DataRow>();
  149. foreach (DataRow row in dt.Rows)
  150. {
  151. bool isEmpty = true;
  152. foreach (var item in row.ItemArray)
  153. {
  154. if (!string.IsNullOrWhiteSpace(item?.ToString()))
  155. {
  156. isEmpty = false;
  157. break;
  158. }
  159. }
  160. if (isEmpty) delRows.Add(row);
  161. }
  162. foreach (var row in delRows) dt.Rows.Remove(row);
  163. }
  164. }
  165. }