zly 4 mēneši atpakaļ
vecāks
revīzija
e773cd653e
1 mainītis faili ar 29 papildinājumiem un 51 dzēšanām
  1. 29 51
      TeamAAS-VM/Models/PLC/PlcExcelLoad.cs

+ 29 - 51
TeamAAS-VM/Models/PLC/PlcExcelLoad.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Collections.Generic;
 using System.Data;
+using System.Formats.Asn1;
 using System.IO;
 using System.Linq;
 using System.Text;
@@ -24,7 +25,7 @@ namespace TeamAAS_VP.Models.PLC
         }
 
         // 默认PLC地址表路径
-        private string _filePath = Path.Combine(Path.GetDirectoryName(Application.StartupPath), @"Configuration Files\PLC地址表.xlsx");
+        private string _filePath = Path.Combine(Path.GetDirectoryName(Application.StartupPath), @"Config\PLC地址表.xlsx");
 
         // 所有集合统一使用 PLCAddrs
         public List<PLCAddrs> IOInputList = new List<PLCAddrs>();
@@ -48,34 +49,36 @@ namespace TeamAAS_VP.Models.PLC
                 _filePath = filePathStr;
         }
 
-        // Excel工作表转DataTable
-        public DataTable GetSheetData(int sheetIndex)
+        private DataTable GetCsvData()
         {
             DataTable dt = new DataTable();
             try
             {
-                using (var fs = new FileStream(_filePath, FileMode.Open, FileAccess.Read))
-                {
-                    IWorkbook workbook = new XSSFWorkbook(fs);
-                    ISheet sheet = workbook.GetSheetAt(sheetIndex);
-                    if (sheet == null || sheet.GetRow(0) == null) return null;
-
-                    IRow headerRow = sheet.GetRow(0);
-                    for (int i = 0; i < headerRow.LastCellNum; i++)
-                        dt.Columns.Add(headerRow.GetCell(i)?.ToString()?.Trim() ?? $"Column{i}");
+                // 优先用GB2312兼容Excel导出的CSV,乱码时可改为Encoding.UTF8/Encoding.Default
+                Encoding encoding = Encoding.GetEncoding("GB2312");
+                string[] lines = File.ReadAllLines(_filePath, encoding);
 
-                    for (int i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; i++)
-                    {
-                        IRow row = sheet.GetRow(i);
-                        if (row == null) continue;
+                if (lines.Length == 0) return null;
 
-                        DataRow dataRow = dt.NewRow();
-                        for (int j = 0; j < dt.Columns.Count; j++)
-                            dataRow[j] = row.GetCell(j)?.ToString()?.Trim() ?? "";
+                // 读取表头
+                string[] headers = lines[0].Split(',');
+                foreach (string header in headers)
+                {
+                    dt.Columns.Add(header.Trim());
+                }
 
-                        dt.Rows.Add(dataRow);
+                // 读取数据行
+                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
@@ -83,16 +86,19 @@ namespace TeamAAS_VP.Models.PLC
                 return null;
             }
         }
-
         /// <summary>
-        /// 加载Excel数据(全部是真实 bool 变量,无字符串类型)
+        /// 加载Excel数据
         /// </summary>
         public void LoadData(int sheetIndex)
         {
             try
             {
                 if (!File.Exists(_filePath)) return;
-                DataTable dt = GetSheetData(sheetIndex);
+
+                DataTable dt = null;
+                string ext = Path.GetExtension(_filePath).ToLower();
+                dt = GetCsvData();
+      
                 if (dt == null || dt.Rows.Count == 0) return;
 
                 RemoveEmptyRow(dt);
@@ -179,34 +185,6 @@ namespace TeamAAS_VP.Models.PLC
             foreach (var row in delRows) dt.Rows.Remove(row);
         }
 
-        // 安全写入Excel
-        public void SaveCellValue(string sheetName, int rowIndex, int colIndex, object value)
-        {
-            if (!File.Exists(_filePath))
-            {
-                MessageBox.Show("PLC地址表不存在!");
-                return;
-            }
-
-            try
-            {
-                using (FileStream fs = new FileStream(_filePath, FileMode.Open, FileAccess.ReadWrite))
-                {
-                    IWorkbook workbook = new XSSFWorkbook(fs);
-                    ISheet sheet = workbook.GetSheet(sheetName) ?? workbook.CreateSheet(sheetName);
-                    IRow row = sheet.GetRow(rowIndex) ?? sheet.CreateRow(rowIndex);
-                    ICell cell = row.GetCell(colIndex) ?? row.CreateCell(colIndex);
-                    cell.SetCellValue(value?.ToString() ?? "");
-
-                    fs.SetLength(0);
-                    workbook.Write(fs);
-                }
-            }
-            catch (Exception ex)
-            {
-                MessageBox.Show($"Excel写入失败:{ex.Message}");
-            }
-        }
     }