| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Formats.Asn1;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using NPOI.SS.UserModel;
- using NPOI.XSSF.UserModel;
- namespace TeamAAS_VP.Models.PLC
- {
- public class PlcExcelLoad
- {
- /// <summary>
- /// 真正的 bool 类型地址结构(无字符串类型)
- /// </summary>
- public class PLCAddrs
- {
- public string OpcAddress { get; set; } // PLC地址(变量名)
- public string Name { get; set; } // 报警文本
- public bool Value { get; set; } // 真实 BOOL 类型值
- }
- // 默认PLC地址表路径
- private string _filePath = Path.Combine(Path.GetDirectoryName(Application.StartupPath), @"Config\PLC地址表.xlsx");
- // 所有集合统一使用 PLCAddrs
- public List<PLCAddrs> IOInputList = new List<PLCAddrs>();
- public List<PLCAddrs> IOOutputList = new List<PLCAddrs>();
- public List<PLCAddrs> JoggingList = new List<PLCAddrs>();
- public List<PLCAddrs> AlarmMaskList = new List<PLCAddrs>();
- public List<PLCAddrs> InputParamList = new List<PLCAddrs>();
- public List<PLCAddrs> OutputParamList = new List<PLCAddrs>();
- public List<PLCAddrs> AlaemList = new List<PLCAddrs>(); // 报警列表
- public List<PLCAddrs> SysStateList = new List<PLCAddrs>(); // 系统状态
- public string FilePath
- {
- get => _filePath;
- set => _filePath = value;
- }
- public PlcExcelLoad(string filePathStr = null)
- {
- if (!string.IsNullOrEmpty(filePathStr))
- _filePath = filePathStr;
- }
- private DataTable GetCsvData()
- {
- DataTable dt = new DataTable();
- try
- {
- // 优先用GB2312兼容Excel导出的CSV,乱码时可改为Encoding.UTF8/Encoding.Default
- Encoding encoding = Encoding.GetEncoding("GB2312");
- string[] lines = File.ReadAllLines(_filePath, encoding);
- if (lines.Length == 0) return null;
- // 读取表头
- string[] headers = lines[0].Split(',');
- foreach (string header in headers)
- {
- dt.Columns.Add(header.Trim());
- }
- // 读取数据行
- for (int i = 1; i < lines.Length; i++)
- {
- string[] cells = lines[i].Split(',');
- DataRow row = dt.NewRow();
- for (int j = 0; j < dt.Columns.Count; j++)
- {
- row[j] = j < cells.Length ? cells[j].Trim() : "";
- }
- dt.Rows.Add(row);
- }
- return dt;
- }
- catch
- {
- return null;
- }
- }
- /// <summary>
- /// 加载Excel数据
- /// </summary>
- public void LoadData(int sheetIndex)
- {
- try
- {
- if (!File.Exists(_filePath)) return;
- DataTable dt = null;
- string ext = Path.GetExtension(_filePath).ToLower();
- dt = GetCsvData();
-
- if (dt == null || dt.Rows.Count == 0) return;
- RemoveEmptyRow(dt);
- foreach (DataRow row in dt.Rows)
- {
- PLCAddrs temp = new PLCAddrs();
- switch (sheetIndex)
- {
- case 0: // IO输入
- temp.OpcAddress = row[0].ToString().Trim();
- temp.Name = row[1].ToString().Trim();
- IOInputList.Add(temp);
- break;
- case 1: // IO输出
- temp.OpcAddress = row[0].ToString().Trim();
- temp.Name = row[1].ToString().Trim();
- IOOutputList.Add(temp);
- break;
- case 2: // 点动
- temp.OpcAddress = row[0].ToString().Trim();
- temp.Name = row[1].ToString().Trim();
- JoggingList.Add(temp);
- break;
- case 3: // 参数输入
- temp.OpcAddress = row[0].ToString().Trim();
- temp.Name = row[1].ToString().Trim();
- InputParamList.Add(temp);
- break;
- case 4: // 参数输出
- temp.OpcAddress = row[0].ToString().Trim();
- temp.Name = row[1].ToString().Trim();
- OutputParamList.Add(temp);
- break;
- case 5: // 报警屏蔽
- temp.OpcAddress = row[0].ToString().Trim();
- temp.Name = row[1].ToString().Trim();
- AlarmMaskList.Add(temp);
- break;
- case 6: // 报警信息(A列名称,B列地址)
- temp.Name = row[0].ToString().Trim(); // 报警文本
- temp.OpcAddress = row[1].ToString().Trim(); // PLC地址
- AlaemList.Add(temp);
- break;
- case 7: // 系统状态
- temp.OpcAddress = row[0].ToString().Trim();
- temp.Name = row[1].ToString().Trim();
- SysStateList.Add(temp);
- break;
- }
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show($"加载PLC地址表失败:{ex.Message}");
- }
- }
- // 清除空行
- public void RemoveEmptyRow(DataTable dt)
- {
- List<DataRow> delRows = new List<DataRow>();
- foreach (DataRow row in dt.Rows)
- {
- bool isEmpty = true;
- foreach (var item in row.ItemArray)
- {
- if (!string.IsNullOrWhiteSpace(item?.ToString()))
- {
- isEmpty = false;
- break;
- }
- }
- if (isEmpty) delRows.Add(row);
- }
- foreach (var row in delRows) dt.Rows.Remove(row);
- }
- }
- }
|