Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Sonic853 committed Jan 3, 2025
1 parent 8fe79f1 commit 8b3e55d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Excel2JsonEX/Excel2JsonEX.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Company>Sonic853</Company>
<AssemblyVersion>1.0.3</AssemblyVersion>
<AssemblyVersion>1.0.4</AssemblyVersion>
<LangVersion>latest</LangVersion>
</PropertyGroup>

Expand Down
54 changes: 39 additions & 15 deletions Excel2JsonEX/JsonExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,18 +205,11 @@ private Dictionary<string, object> 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);
Expand All @@ -228,17 +221,22 @@ private Dictionary<string, object> 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<List<Dictionary<string, string>>>(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();
// 表头自动转换成小写
Expand All @@ -255,15 +253,22 @@ private Dictionary<string, object> 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":
Expand Down Expand Up @@ -299,6 +304,25 @@ private Dictionary<string, object> 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,
};
}

/// <summary>
/// 对于表格中的空值,找到一列中的非空值,并构造一个同类型的默认值
/// </summary>
Expand Down

0 comments on commit 8b3e55d

Please sign in to comment.