From 12e606635c42ea20f4b7c2bdda5204237a4b4c96 Mon Sep 17 00:00:00 2001
From: alielie <alielie@git.com>
Date: 星期一, 02 三月 2026 16:14:49 +0800
Subject: [PATCH] 优化

---
 HMI.Util/ExcelHelper.cs |  140 ++++++++++++++++++++++++++--------------------
 1 files changed, 78 insertions(+), 62 deletions(-)

diff --git a/HMI.Util/ExcelHelper.cs b/HMI.Util/ExcelHelper.cs
index b2ee156..9e795dc 100644
--- a/HMI.Util/ExcelHelper.cs
+++ b/HMI.Util/ExcelHelper.cs
@@ -43,8 +43,9 @@
                 IRow rowTemp = SheetOne.CreateRow(i + 1);
                 for (int j = 0; j < properties.Count; j++)
                 {
-                    var value = properties[j].GetValue(datas[i]);
-                    rowTemp.CreateCell(j).SetCellValue(value.ToString());
+                    var attr = attrs[j];
+                    var value = attr.IgnoreExport ? "" : properties[j].GetValue(datas[i]);
+                    rowTemp.CreateCell(j).SetCellValue(value?.ToString());
                 }
             }
 
@@ -71,71 +72,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))
-                            {
-                                CellType ct = row.GetCell(j).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