|
|
@ -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<TImportModel> Import<TImportModel>(byte[] bytes) |
|
|
|
public IList<TImportModel> Import<TImportModel>(Stream stream) |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
var result = new List<TImportModel>(); |
|
|
|
//
|
|
|
|
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<ImporterHeaderAttribute>()?.Name ?? o.GetCustomAttribute<DisplayAttribute>()?.Name ?? o.Name); |
|
|
|
var properties = GetProperties(type).ToDictionary(o => o.GetCustomAttribute<ImporterHeaderAttribute>()?.Name ?? o.GetDisplayName() ?? o.Name); |
|
|
|
var name = type.GetCustomAttribute<DisplayAttribute>()?.Name ?? typeof(TImportModel).Name; |
|
|
|
var ws = workbook.Worksheets.FirstOrDefault(); |
|
|
|
if (ws != null) |
|
|
@ -121,14 +122,71 @@ 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(); |
|
|
|
properties.TryGetValue(headerName, out var property); |
|
|
|
if (property != null) |
|
|
|
|
|
|
|
var value = cell.Value.ToString().Trim(); |
|
|
|
if (!string.IsNullOrEmpty(value) && properties.TryGetValue(headerName, out var property)) |
|
|
|
{ |
|
|
|
SetProperty(model, property, value); |
|
|
|
} |
|
|
|
} |
|
|
|
result.Add(model); |
|
|
|
} |
|
|
|
} |
|
|
|
return result; |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
this._logger.LogError(ex, ex.ToString()); |
|
|
|
throw new Exception($"导入数据错误:{ex.Message}", ex); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private static PropertyInfo[] GetProperties(Type type) |
|
|
|
{ |
|
|
|
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<ScaffoldColumnAttribute>(true).Any()) |
|
|
|
.Where(o => !o.GetCustomAttributes<ImporterHeaderAttribute>(true).Any() || !o.GetCustomAttribute<ImporterHeaderAttribute>(true)!.IsIgnore) |
|
|
|
.ToArray(); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 导出设置单元格
|
|
|
|
/// </summary>
|
|
|
|
private static void SetCell<TExportModel>(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; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 导入设置属性值
|
|
|
|
/// </summary>
|
|
|
|
private static void SetProperty<TImopotModel>(TImopotModel model, PropertyInfo property, string value) |
|
|
|
{ |
|
|
|
var propertyType = property.PropertyType; |
|
|
|
var propertyType = property.PropertyType.GetUnderlyingType(); |
|
|
|
if (propertyType.IsEnum) |
|
|
|
{ |
|
|
|
var enumValue = Enum.GetNames(propertyType) |
|
|
@ -138,9 +196,9 @@ public class ClosedXmlExportImportService : IExportImportService |
|
|
|
.FirstOrDefault(); |
|
|
|
property.SetValue(model, enumValue); |
|
|
|
} |
|
|
|
else if (propertyType.Name == nameof(Boolean)) |
|
|
|
else if (propertyType == typeof(bool)) |
|
|
|
{ |
|
|
|
if (value.GetText() == "是") |
|
|
|
if (value == "是") |
|
|
|
{ |
|
|
|
property.SetValue(model, true); |
|
|
|
} |
|
|
@ -149,30 +207,14 @@ public class ClosedXmlExportImportService : IExportImportService |
|
|
|
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); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
result.Add(model); |
|
|
|
} |
|
|
|
} |
|
|
|
return result; |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
this._logger.LogError(ex, ex.ToString()); |
|
|
|
throw new Exception($"导入数据错误:{ex.Message}", ex); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private static PropertyInfo[] GetPropertiesForImportModel(Type type) |
|
|
|
{ |
|
|
|
return type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty) |
|
|
|
.Where(o => o.GetCustomAttribute<ImporterHeaderAttribute>(true) == null || !o.GetCustomAttribute<ImporterHeaderAttribute>()!.IsIgnore) |
|
|
|
.ToArray(); |
|
|
|
} |
|
|
|
} |
|
|
|