diff --git a/docs/demo/src/WTA.Shared/Controllers/GenericController.cs b/docs/demo/src/WTA.Shared/Controllers/GenericController.cs index 751810b6..cea4d81c 100644 --- a/docs/demo/src/WTA.Shared/Controllers/GenericController.cs +++ b/docs/demo/src/WTA.Shared/Controllers/GenericController.cs @@ -197,10 +197,17 @@ public class GenericController(); + var list = exportImportService.Import(file.OpenReadStream()); + list.ForEach(item => + { + this.Repository.Insert(item.ToObject()); + }); + this.Repository.SaveChanges(); return NoContent(); } catch (Exception ex) diff --git a/docs/demo/src/WTA.Shared/ExportImport/ClosedXmlExportImportService.cs b/docs/demo/src/WTA.Shared/ExportImport/ClosedXmlExportImportService.cs index c8924169..4f735756 100644 --- a/docs/demo/src/WTA.Shared/ExportImport/ClosedXmlExportImportService.cs +++ b/docs/demo/src/WTA.Shared/ExportImport/ClosedXmlExportImportService.cs @@ -37,7 +37,7 @@ public class ClosedXmlExportImportService : IExportImportService var ws = workbook.Worksheets.Add(name); // var type = typeof(TExportModel); - var propertyList = GetPropertiesForImportModel(type); + var propertyList = GetProperties(type); var rowIndex = 1; for (var i = 0; i < propertyList.Length; i++) { @@ -53,8 +53,9 @@ public class ClosedXmlExportImportService : IExportImportService { var property = propertyList[i]; var columnIndex = i + 1; + var value = property.GetValue(model)?.ToString(); var cell = ws.Cell(rowIndex, columnIndex); - cell.Value = property.GetValue(model)?.ToString(); + SetCell(model, cell, property); } }); // @@ -81,7 +82,7 @@ public class ClosedXmlExportImportService : IExportImportService var name = type.GetDisplayName(); var fileName = $"{name}_导入模板.xlsx"; var ws = workbook.Worksheets.Add(name); - var properties = GetPropertiesForImportModel(type); + var properties = GetProperties(type); for (var i = 0; i < properties.Length; i++) { var property = properties[i]; @@ -99,15 +100,15 @@ public class ClosedXmlExportImportService : IExportImportService return result; } - public IList Import(byte[] bytes) + public IList Import(Stream stream) { try { var result = new List(); // - using var workbook = new XLWorkbook(new MemoryStream(bytes)); + using var workbook = new XLWorkbook(stream); var type = typeof(TImportModel); - var properties = GetPropertiesForImportModel(type).ToDictionary(o => o.GetCustomAttribute()?.Name ?? o.GetCustomAttribute()?.Name ?? o.Name); + var properties = GetProperties(type).ToDictionary(o => o.GetCustomAttribute()?.Name ?? o.GetDisplayName() ?? o.Name); var name = type.GetCustomAttribute()?.Name ?? typeof(TImportModel).Name; var ws = workbook.Worksheets.FirstOrDefault(); if (ws != null) @@ -121,40 +122,12 @@ public class ClosedXmlExportImportService : IExportImportService { var columnIndex = j + 1; var cell = row.Cell(columnIndex); - var value = cell.Value; - if (value.ToString() != "") + var headerName = ws.Cell(1, columnIndex).Value.ToString().Trim(); + + var value = cell.Value.ToString().Trim(); + if (!string.IsNullOrEmpty(value) && properties.TryGetValue(headerName, out var property)) { - var headerName = ws.Cell(1, columnIndex).Value.ToString().Trim(); - properties.TryGetValue(headerName, out var property); - if (property != null) - { - var propertyType = property.PropertyType; - if (propertyType.IsEnum) - { - var enumValue = Enum.GetNames(propertyType) - .Select(o => new KeyValuePair(o, (Enum)Enum.Parse(propertyType, o))) - .Where(o => o.Value.GetDisplayName() == value.ToString()) - .Select(o => o.Value) - .FirstOrDefault(); - property.SetValue(model, enumValue); - } - else if (propertyType.Name == nameof(Boolean)) - { - if (value.GetText() == "是") - { - property.SetValue(model, true); - } - else - { - property.SetValue(model, false); - } - } - else - { - var propertyValue = Convert.ChangeType(value.ToString(), propertyType, CultureInfo.InvariantCulture); - property.SetValue(model, propertyValue); - } - } + SetProperty(model, property, value); } } result.Add(model); @@ -169,10 +142,79 @@ public class ClosedXmlExportImportService : IExportImportService } } - private static PropertyInfo[] GetPropertiesForImportModel(Type type) + private static PropertyInfo[] GetProperties(Type type) { - return type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty) - .Where(o => o.GetCustomAttribute(true) == null || !o.GetCustomAttribute()!.IsIgnore) + var properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty); + return properties + .Where(o => o.PropertyType == typeof(string) || o.PropertyType.IsNullableType() || o.PropertyType.IsValueType) + .Where(o => !o.GetCustomAttributes(true).Any()) + .Where(o => !o.GetCustomAttributes(true).Any() || !o.GetCustomAttribute(true)!.IsIgnore) .ToArray(); } + + /// + /// 导出设置单元格 + /// + private static void SetCell(TExportModel? model, IXLCell cell, PropertyInfo property) + { + var propertyType = property.PropertyType.GetUnderlyingType(); + var value = property.GetValue(model)?.ToString()?.Trim(); + if (string.IsNullOrEmpty(value)) + { + return; + } + if (propertyType == typeof(bool)) + { + cell.Value = (bool)property.GetValue(model)! ? "是" : "否"; + } + else if (propertyType.IsEnum) + { + cell.Value = (Enum.Parse(propertyType, value) as Enum)?.GetDisplayName(); + } + else if (propertyType == typeof(DateTime)) + { + cell.Value = ((DateTime)property.GetValue(model)!).ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture); + } + else + { + cell.Value = value; + } + } + + /// + /// 导入设置属性值 + /// + private static void SetProperty(TImopotModel model, PropertyInfo property, string value) + { + var propertyType = property.PropertyType.GetUnderlyingType(); + if (propertyType.IsEnum) + { + var enumValue = Enum.GetNames(propertyType) + .Select(o => new KeyValuePair(o, (Enum)Enum.Parse(propertyType, o))) + .Where(o => o.Value.GetDisplayName() == value.ToString()) + .Select(o => o.Value) + .FirstOrDefault(); + property.SetValue(model, enumValue); + } + else if (propertyType == typeof(bool)) + { + if (value == "是") + { + property.SetValue(model, true); + } + else + { + property.SetValue(model, false); + } + } + else if (propertyType == typeof(Guid)) + { + property.SetValue(model, Guid.Parse(value)); + } + else + { + var propertyValue = Convert.ChangeType(value.ToString(), propertyType, CultureInfo.InvariantCulture); + property.SetValue(model, propertyValue); + } + } } diff --git a/docs/demo/src/WTA.Shared/ExportImport/IExportImportService.cs b/docs/demo/src/WTA.Shared/ExportImport/IExportImportService.cs index adb43efc..864de43d 100644 --- a/docs/demo/src/WTA.Shared/ExportImport/IExportImportService.cs +++ b/docs/demo/src/WTA.Shared/ExportImport/IExportImportService.cs @@ -8,5 +8,5 @@ public interface IExportImportService FileContentResult GetImportTemplate(); - IList Import(byte[] bytes); + IList Import(Stream stream); } diff --git a/docs/demo/src/WTA.Shared/Monitor/MonitorHostedService.cs b/docs/demo/src/WTA.Shared/Monitor/MonitorHostedService.cs index 3dc51c33..f138bc5c 100644 --- a/docs/demo/src/WTA.Shared/Monitor/MonitorHostedService.cs +++ b/docs/demo/src/WTA.Shared/Monitor/MonitorHostedService.cs @@ -34,7 +34,7 @@ public class MonitorHostedService : IHostedService { Debug.WriteLine(ex.ToString()); } - await Task.Delay(1000 * 1).ConfigureAwait(false); + await Task.Delay(5000 * 1).ConfigureAwait(false); } }, cancellationToken); return Task.CompletedTask; diff --git a/docs/demo/src/WTA/wwwroot/components/list/index.js b/docs/demo/src/WTA/wwwroot/components/list/index.js index 441f14fc..9da96e1a 100644 --- a/docs/demo/src/WTA/wwwroot/components/list/index.js +++ b/docs/demo/src/WTA/wwwroot/components/list/index.js @@ -194,7 +194,7 @@ export default { v-model="dialogVisible" align-center destroy-on-close - style="width:auto;min-width:300px;max-width:700px;" + style="width:auto;min-width:500px;max-width:1000px;" > @@ -214,7 +214,7 @@ export default { />