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
{
///
/// 真正的 bool 类型地址结构(无字符串类型)
///
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 IOInputList = new List();
public List IOOutputList = new List();
public List JoggingList = new List();
public List AlarmMaskList = new List();
public List InputParamList = new List();
public List OutputParamList = new List();
public List AlaemList = new List(); // 报警列表
public List SysStateList = new List(); // 系统状态
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;
}
}
///
/// 加载Excel数据
///
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 delRows = new List();
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);
}
}
}