diff --git a/IRaCIS.Core.Application/Helper/ExcelExportHelper.cs b/IRaCIS.Core.Application/Helper/ExcelExportHelper.cs index 3417cb7c2..178109b5d 100644 --- a/IRaCIS.Core.Application/Helper/ExcelExportHelper.cs +++ b/IRaCIS.Core.Application/Helper/ExcelExportHelper.cs @@ -16,6 +16,7 @@ using NPOI.HPSF; using NPOI.HSSF.UserModel; using NPOI.XSSF.UserModel; using SkiaSharp; +using System.Collections; using System.IO; namespace IRaCIS.Core.Application.Service; @@ -52,21 +53,24 @@ public static class ExcelExportHelper var translateDataList = await _dictionaryService.GetBasicDataSelect(needTranslatePropertyList.Select(t => t.DicParentCode).Distinct().ToArray()); - var dic = (JsonConvert.DeserializeObject>(data.ToJsonNotIgnoreNull())).IfNullThrowException(); + var dic = data.ConvertToDictionary(); foreach (var key in dic.Keys) { //是数组 那么找到对应的属性 进行翻译 - if (dic[key].GetType().IsAssignableFrom(typeof(JArray))) + if (dic[key].GetType().GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IList<>))) { var newObjList = new List(); var no = 1; - foreach (var item in dic[key] as JArray) + foreach (var item in dic[key] as IList ) { - var itemDic = JsonConvert.DeserializeObject>(item.ToJsonNotIgnoreNull()); + //var itemDic = JsonConvert.DeserializeObject>(item.ToJsonNotIgnoreNull()); + + var itemDic = item.ConvertToDictionary(); + foreach (var needTranslateProperty in needTranslatePropertyList) @@ -74,7 +78,7 @@ public static class ExcelExportHelper //翻译的属性依赖其他属性 if (needTranslateProperty.IsTranslateDenpendOtherProperty) { - if (item[needTranslateProperty.DependPropertyName]?.ToString().ToLower() == needTranslateProperty.DependPropertyValueStr.ToLower()) + if (itemDic[needTranslateProperty.DependPropertyName]?.ToString().ToLower() == needTranslateProperty.DependPropertyValueStr.ToLower()) { var beforeValue = itemDic[needTranslateProperty.Name]?.ToString(); @@ -232,29 +236,30 @@ public static class ExcelExportHelper var translateDataList = await _dictionaryService.GetBasicDataSelect(needTranslatePropertyList.Select(t => t.DicParentCode).Distinct().ToArray()); - - var dic = (JsonConvert.DeserializeObject>(data.ToJsonNotIgnoreNull())).IfNullThrowException(); + var dic = data.ConvertToDictionary(); + //var dic = (JsonConvert.DeserializeObject>(data.ToJsonNotIgnoreNull())).IfNullThrowException(); foreach (var key in dic.Keys) { //是数组 那么找到对应的属性 进行翻译 - if (dic[key].GetType().IsAssignableFrom(typeof(JArray))) + if (dic[key].GetType().GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IList<>))) + //if (dic[key].GetType().IsAssignableFrom(typeof(JArray))) { var newObjList = new List(); var no = 1; - - foreach (var item in dic[key] as JArray) + foreach (var item in dic[key] as IList) + //foreach (var item in dic[key] as JArray) { - var itemDic = JsonConvert.DeserializeObject>(item.ToJsonNotIgnoreNull()); - + //var itemDic = JsonConvert.DeserializeObject>(item.ToJsonNotIgnoreNull()); + var itemDic = item.ConvertToDictionary(); foreach (var needTranslateProperty in needTranslatePropertyList) { //翻译的属性依赖其他属性 if (needTranslateProperty.IsTranslateDenpendOtherProperty) { - if (item[needTranslateProperty.DependPropertyName]?.ToString().ToLower() == needTranslateProperty.DependPropertyValueStr.ToLower()) + if (itemDic[needTranslateProperty.DependPropertyName]?.ToString().ToLower() == needTranslateProperty.DependPropertyValueStr.ToLower()) { var beforeValue = itemDic[needTranslateProperty.Name]?.ToString(); diff --git a/IRaCIS.Core.Infrastructure/_IRaCIS/ObjectExtension.cs b/IRaCIS.Core.Infrastructure/_IRaCIS/ObjectExtension.cs index adfc82153..c861068de 100644 --- a/IRaCIS.Core.Infrastructure/_IRaCIS/ObjectExtension.cs +++ b/IRaCIS.Core.Infrastructure/_IRaCIS/ObjectExtension.cs @@ -1,5 +1,7 @@ using Newtonsoft.Json; using System; +using System.Collections.Generic; +using System.Reflection; namespace IRaCIS.Core.Infrastructure.Extention { @@ -38,7 +40,30 @@ namespace IRaCIS.Core.Infrastructure.Extention public static string ToJsonNotIgnoreNull(this object obj) { - return JsonConvert.SerializeObject(obj, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss", ReferenceLoopHandling = ReferenceLoopHandling.Ignore, NullValueHandling = NullValueHandling.Include }); + return JsonConvert.SerializeObject(obj, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss", Formatting = Formatting.Indented, ReferenceLoopHandling = ReferenceLoopHandling.Ignore, NullValueHandling = NullValueHandling.Include }); } - } + + public static Dictionary ConvertToDictionary(this object obj) + { + if (obj == null) + { + throw new ArgumentNullException(nameof(obj)); + } + + Type type = obj.GetType(); + PropertyInfo[] properties = type.GetProperties(); + + Dictionary dictionary = new Dictionary(); + + foreach (PropertyInfo property in properties) + { + string propertyName = property.Name; + object propertyValue = property.GetValue(obj); + + dictionary.Add(propertyName, propertyValue); + } + + return dictionary; + } + } }