From fe2aa4880d5c9fd88088ad0ed81ebaca05629f5f Mon Sep 17 00:00:00 2001
From: wenxiangchao <wenxiangchao@ihengxin.com.cn>
Date: 星期一, 02 十一月 2026 17:59:45 +0800
Subject: [PATCH] feat: 集成CAN驱动/模拟器 + 仪器仪表注册/硬件调试模块

---
 HMI.Util/ExcelHelper.cs |  219 ++++++++++++++++++++++++++++++++++++++----------------
 1 files changed, 155 insertions(+), 64 deletions(-)

diff --git a/HMI.Util/ExcelHelper.cs b/HMI.Util/ExcelHelper.cs
index ee8f3fc..801b657 100644
--- a/HMI.Util/ExcelHelper.cs
+++ b/HMI.Util/ExcelHelper.cs
@@ -1,10 +1,12 @@
 锘縰sing HMI.Core;
 using HMI.Core.Util;
 using NPOI.HSSF.UserModel;
+using NPOI.SS.Formula.Functions;
 using NPOI.SS.UserModel;
 using NPOI.XSSF.UserModel;
 using System;
 using System.Collections.Generic;
+using System.Data;
 using System.IO;
 using System.Linq;
 using System.Reflection;
@@ -45,7 +47,84 @@
                 {
                     var attr = attrs[j];
                     var value = attr.IgnoreExport ? "" : properties[j].GetValue(datas[i]);
-                    rowTemp.CreateCell(j).SetCellValue(value.ToString());
+                    rowTemp.CreateCell(j).SetCellValue(value?.ToString());
+                }
+            }
+
+            using (FileStream fs = new FileStream(targetPath, FileMode.OpenOrCreate))
+            {
+                workbook.Write(fs);
+            }
+        }
+
+        public static void ExportExcel<T>(string targetPath, DataTable table)
+        {
+            Type type = typeof(T);
+            var properties = PropertyHelper.GetAtterProperty<T, ExcelColumnAttribute>(out var attrs);
+            if (properties == null || properties.Count <= 0)
+            {
+                throw new Exception("No Attribute");
+            }
+            IWorkbook workbook = null;
+            if (targetPath.EndsWith(".xlsx"))
+            {
+                workbook = new XSSFWorkbook();
+            }
+            else if (targetPath.EndsWith(".xls"))
+            {
+                workbook = new HSSFWorkbook();
+            }
+            workbook.CreateSheet("sheet1");////鏂板缓涓�涓伐浣滆〃
+            ISheet SheetOne = workbook.GetSheet("Sheet1");
+            IRow row1 = SheetOne.CreateRow(0);
+            for (int i = 0; i < properties.Count; i++)
+            {
+                row1.CreateCell(i).SetCellValue(LangUtil.Get((attrs[i]).Name));
+            }
+
+            for (int i = 0; i < table.Rows.Count; i++)
+            {
+                IRow rowTemp = SheetOne.CreateRow(i + 1);
+                for (int j = 0; j < properties.Count; j++)
+                {
+                    var attr = attrs[j];
+                    object obj = attr.IgnoreExport ? "" : table.Rows[i][properties[j].Name];
+                    string value = obj?.ToString();
+                    if (obj is DateTime time)
+                    {
+                        value = time.ToString("yyyy-MM-dd HH:mm:ss");
+                    }
+                    rowTemp.CreateCell(j).SetCellValue(value);
+                }
+            }
+
+            using (FileStream fs = new FileStream(targetPath, FileMode.OpenOrCreate))
+            {
+                workbook.Write(fs);
+            }
+        }
+
+        public static void ExportExcel(string targetPath, List<string[]> rows)
+        {
+            IWorkbook workbook = null;
+            if (targetPath.EndsWith(".xlsx"))
+            {
+                workbook = new XSSFWorkbook();
+            }
+            else if (targetPath.EndsWith(".xls"))
+            {
+                workbook = new HSSFWorkbook();
+            }
+            workbook.CreateSheet("sheet1");////鏂板缓涓�涓伐浣滆〃
+            ISheet SheetOne = workbook.GetSheet("sheet1");
+
+
+            for (int i = 0; i < rows.Count; i++)
+            {
+                IRow rowTemp = SheetOne.CreateRow(i);
+                for (int j = 0; j < rows[i].Length; j++)
+                {
+                    rowTemp.CreateCell(j).SetCellValue(rows[i][j]);
                 }
             }
 
@@ -72,74 +151,86 @@
             using (FileStream fileStream1 = new FileStream(sourcePath, FileMode.Open, FileAccess.Read))
             {
                 IWorkbook workbook = null;
-                if (sourcePath.EndsWith(".xlsx"))
-                {
-                    workbook = new XSSFWorkbook(fileStream1);
-                }
-                else if (sourcePath.EndsWith(".xls"))
+                if (sourcePath.EndsWith(".xls"))
                 {
                     workbook = new HSSFWorkbook(fileStream1);
                 }
-
-                ISheet sheet = workbook.GetSheetAt(0);
-                IRow titleRow = sheet.GetRow(0);
-
-                List<T> result = new List<T>();
-                IRow row;
-                for (int i = 1; i < (sheet.LastRowNum + 1); i++)
+                else
                 {
-                    row = sheet.GetRow(i);
-                    if (row != null)
-                    {
-                        T obj = (T)Activator.CreateInstance(type);
-                        bool isEmpty = true;
-                        for (int j = 0; j < row.Count(); j++)
-                        {
-                            string title = titleRow.GetCell(j).StringCellValue;
-                            if (dict.ContainsKey(title))
-                            {
-                                var cell = row.GetCell(j);
-                                if (cell == null || cell.CellType == CellType.Blank)
-                                    continue;
-                                CellType ct = cell.CellType;
-                                object value = null;
-                                if (ct == CellType.String)
-                                    value = row.GetCell(j).StringCellValue;
-                                else if (ct == CellType.Numeric)
-                                    value = row.GetCell(j).NumericCellValue;
-                                else if (ct == CellType.Boolean)
-                                    value = row.GetCell(j).BooleanCellValue;
-
-                                var prop = dict[title];
-                                if (prop.PropertyType == typeof(string))
-                                {
-                                    value = Convert.ToString(value);
-                                }
-                                else if (prop.PropertyType == typeof(double) || prop.PropertyType == typeof(double?))
-                                {
-                                    value = Convert.ToDouble(value);
-                                }
-                                else if (prop.PropertyType == typeof(int) || prop.PropertyType == typeof(int?))
-                                {
-                                    value = Convert.ToInt16(value);
-                                }
-                                else if (prop.PropertyType == typeof(bool) || prop.PropertyType == typeof(bool?))
-                                {
-                                    value = Convert.ToBoolean(value);
-                                }
-                                else if (prop.PropertyType.IsEnum)
-                                {
-                                    value = Enum.Parse(prop.PropertyType, value.ToString());
-                                }
-                                prop.SetValue(obj, value);
-                                isEmpty = false;
-                            }
-                        }
-                        if (!isEmpty) result.Add(obj);
-                    }
+                    workbook = new XSSFWorkbook(fileStream1);
                 }
-                workbook.Close();
-                return result;
+                int currentRow = 1;
+                try
+                {
+                    ISheet sheet = workbook.GetSheetAt(0);
+                    IRow titleRow = sheet.GetRow(0);
+
+                    List<T> result = new List<T>();
+                    IRow row;
+                    for (currentRow = 1; currentRow < (sheet.LastRowNum + 1); currentRow++)
+                    {
+                        row = sheet.GetRow(currentRow);
+                        if (row != null)
+                        {
+                            T obj = (T)Activator.CreateInstance(type);
+                            bool isEmpty = true;
+                            for (int j = 0; j < row.Count(); j++)
+                            {
+                                string title = titleRow.Cells[j].StringCellValue;
+                                if (dict.ContainsKey(title))
+                                {
+                                    var cell = row.GetCell(j);
+                                    if (cell == null || cell.CellType == CellType.Blank)
+                                        continue;
+                                    CellType ct = cell.CellType;
+                                    object value = null;
+                                    if (ct == CellType.String)
+                                        value = row.GetCell(j).StringCellValue;
+                                    else if (ct == CellType.Numeric)
+                                        value = row.GetCell(j).NumericCellValue;
+                                    else if (ct == CellType.Boolean)
+                                        value = row.GetCell(j).BooleanCellValue;
+                                    else
+                                        value = row.GetCell(j).StringCellValue;
+
+                                    var prop = dict[title];
+                                    if (prop.PropertyType == typeof(string))
+                                    {
+                                        value = Convert.ToString(value);
+                                    }
+                                    else if (prop.PropertyType == typeof(double) || prop.PropertyType == typeof(double?))
+                                    {
+                                        value = Convert.ToDouble(value);
+                                    }
+                                    else if (prop.PropertyType == typeof(int) || prop.PropertyType == typeof(int?))
+                                    {
+                                        value = Convert.ToInt16(value);
+                                    }
+                                    else if (prop.PropertyType == typeof(bool) || prop.PropertyType == typeof(bool?))
+                                    {
+                                        value = Convert.ToBoolean(value);
+                                    }
+                                    else if (prop.PropertyType.IsEnum)
+                                    {
+                                        value = Enum.Parse(prop.PropertyType, value.ToString());
+                                    }
+                                    prop.SetValue(obj, value);
+                                    isEmpty = false;
+                                }
+                            }
+                            if (!isEmpty) result.Add(obj);
+                        }
+                    }
+                    return result;
+                }
+                catch (Exception ex)
+                {
+                    throw new Exception($"绗瑊currentRow}琛岃В鏋愰敊璇細{ex.Message}", ex);
+                }
+                finally
+                {
+                    workbook.Close();
+                }
             }
         }
     }

--
Gitblit v1.9.1