diff --git a/Excel2JsonEX/Excel2JsonEX.csproj b/Excel2JsonEX/Excel2JsonEX.csproj
index a44ca98..8abc055 100644
--- a/Excel2JsonEX/Excel2JsonEX.csproj
+++ b/Excel2JsonEX/Excel2JsonEX.csproj
@@ -5,7 +5,7 @@
enable
enable
Sonic853
- 1.0.3
+ 1.0.4
latest
diff --git a/Excel2JsonEX/JsonExporter.cs b/Excel2JsonEX/JsonExporter.cs
index 3f9c37c..77ba9f5 100644
--- a/Excel2JsonEX/JsonExporter.cs
+++ b/Excel2JsonEX/JsonExporter.cs
@@ -205,18 +205,11 @@ private Dictionary convertRowToDict(DataTable sheet, DataRow row
else if (mHeaderRows >= 1)
// 从第二行获取类型
{
- var typeObj = sheet.Rows[0][column];
- var typeString = typeObj.ToString();
- if (!string.IsNullOrEmpty(typeString))
+ var defaultType = getDefaultType(sheet.Rows[0][column], out var isSet);
+ if (isSet)
{
- var defaultType = getDefaultType(typeString, out var isSet);
- if (isSet)
- {
- foundTypeColumn.TryAdd(column, defaultType);
- value = defaultType;
- }
- else
- value = getColumnDefault(sheet, column, mHeaderRows, ref foundTypeColumn);
+ foundTypeColumn.TryAdd(column, defaultType);
+ value = defaultType;
}
else
value = getColumnDefault(sheet, column, mHeaderRows, ref foundTypeColumn);
@@ -228,17 +221,22 @@ private Dictionary convertRowToDict(DataTable sheet, DataRow row
}
else if (value.GetType() == typeof(double))
{ // 去掉数值字段的“.0”
- double num = (double)value;
+ var num = (double)value;
if ((int)num == num)
value = (int)num;
}
+
//全部转换为string
//方便LitJson.JsonMapper.ToObject>>(textAsset.text)等使用方式 之后根据自己的需求进行解析
if (options.AllString && value is not string)
{
value = value?.ToString();
}
+ else if (mHeaderRows >= 1)
+ {
+ value = getValueByType(value, sheet.Rows[0][column]);
+ }
var fieldName = column.ToString();
// 表头自动转换成小写
@@ -255,15 +253,22 @@ private Dictionary convertRowToDict(DataTable sheet, DataRow row
return rowData;
}
- static object? getDefaultType(string typeString, out bool isSet)
+ static object? getDefaultType(object typeObj, out bool isSet)
{
- isSet = false;
+
+ var typeString = typeObj.ToString();
+ if (string.IsNullOrEmpty(typeString))
+ {
+ isSet = false;
+ return null;
+ }
switch (typeString.ToLower())
{
case "string":
isSet = true;
- return "";
+ return default(string);
case "boolean":
+ case "bool":
isSet = true;
return default(bool);
case "int":
@@ -299,6 +304,25 @@ private Dictionary convertRowToDict(DataTable sheet, DataRow row
}
}
+ static object? getValueByType(object? value, object typeObj)
+ {
+ var typeString = typeObj.ToString();
+ if (string.IsNullOrEmpty(typeString))
+ {
+ return value;
+ }
+ return typeString.ToLower() switch
+ {
+ "string" => value?.ToString(),
+ "boolean" or "bool" => Convert.ToBoolean(value),
+ "int" => Convert.ToInt32(value),
+ "long" => Convert.ToInt64(value),
+ "float" => Convert.ToSingle(value),
+ "double" => Convert.ToDouble(value),
+ _ => value,
+ };
+ }
+
///
/// 对于表格中的空值,找到一列中的非空值,并构造一个同类型的默认值
///