Sfoglia il codice sorgente

解决 excel 内容为公式类型时 直接cell.ToString 会报错。 必须获取对应类型的值 (#210)

Co-authored-by: cheny <cheny@airflys.com>
cy398088147 5 anni fa
parent
commit
d9f1a231c0

+ 32 - 3
Unity/Assets/Editor/ExcelExporterEditor/ExcelExporterEditor.cs

@@ -343,17 +343,46 @@ namespace ET
 
         private static string GetCellString(ISheet sheet, int i, int j)
         {
-            return sheet.GetRow(i)?.GetCell(j)?.ToString() ?? "";
+            IRow _irow = sheet.GetRow(i);
+            if(_irow != null)
+            {
+               return GetCellString(_irow, j);
+            }
+            return "";
         }
 
         private static string GetCellString(IRow row, int i)
         {
-            return row?.GetCell(i)?.ToString() ?? "";
+            ICell _icell = row.GetCell(i);
+            if (_icell != null)
+            {
+                return GetCellString(_icell); ;
+            }
+            return "";
         }
 
         private static string GetCellString(ICell cell)
         {
-            return cell?.ToString() ?? "";
+            if (cell != null)
+            {
+                if(cell.CellType == CellType.Numeric || (cell.CellType == CellType.Formula && cell.CachedFormulaResultType == CellType.Numeric))
+                {
+                    return cell.NumericCellValue.ToString();
+                }
+                else if (cell.CellType == CellType.String || (cell.CellType == CellType.Formula && cell.CachedFormulaResultType == CellType.String))
+                {
+                    return cell.StringCellValue.ToString();
+                }
+                else if (cell.CellType == CellType.Boolean || (cell.CellType == CellType.Formula && cell.CachedFormulaResultType == CellType.Boolean))
+                {
+                    return cell.BooleanCellValue.ToString();
+                }
+                else
+                {
+                    return cell.ToString();
+                }
+            }
+            return "";
         }
     }
 }