|
|
@ -7,6 +7,7 @@ using System.Linq; |
|
|
|
using System.Linq.Dynamic.Core; |
|
|
|
using System.Reflection; |
|
|
|
using AutoMapper.Internal; |
|
|
|
using ClosedXML; |
|
|
|
using ClosedXML.Excel; |
|
|
|
using ClosedXML.Graphics; |
|
|
|
using DocumentFormat.OpenXml; |
|
|
@ -16,6 +17,7 @@ using Microsoft.EntityFrameworkCore; |
|
|
|
using Volo.Abp.Uow; |
|
|
|
using Win_in.Sfs.Shared.Application.Contracts; |
|
|
|
using Win_in.Sfs.Shared.Application.Contracts.ExportAndImport; |
|
|
|
using Win_in.Sfs.Shared.Domain; |
|
|
|
using Win_in.Sfs.Shared.Domain.Shared; |
|
|
|
|
|
|
|
namespace Win_in.Sfs.Shared.Application; |
|
|
@ -156,6 +158,72 @@ public class ClosedXmlExportImportService : IExportImportService |
|
|
|
var row = ws.Row(rowIndex); |
|
|
|
var model = Activator.CreateInstance<TImportModel>(); |
|
|
|
for (int j = 0; j < ws.ColumnsUsed().Count(); j++) |
|
|
|
{ |
|
|
|
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 PropertyInfo property); |
|
|
|
if (property != null) |
|
|
|
{ |
|
|
|
var propertyType = property.PropertyType; |
|
|
|
if (propertyType.IsEnum) |
|
|
|
{ |
|
|
|
var enumValue = Enum.GetNames(propertyType) |
|
|
|
.Select(o => new KeyValuePair<string, Enum>(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); |
|
|
|
property.SetValue(model, propertyValue); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
result.Add(model); |
|
|
|
} |
|
|
|
return result; |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
throw new Exception($"导入数据错误:{ex.Message}", ex); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public Dictionary<TImportModel, List<ValidationResult>> ImportHaveValidationResult<TImportModel>(byte[] bytes) |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
var result = new Dictionary<TImportModel, List<ValidationResult>>(); |
|
|
|
using var workbook = new XLWorkbook(new MemoryStream(bytes)); |
|
|
|
var type = typeof(TImportModel); |
|
|
|
var properties = GetPropertiesForImportModel(type).ToDictionary(o => o.GetCustomAttribute<ImporterHeaderAttribute>()?.Name ?? o.GetCustomAttribute<DisplayAttribute>()?.Name ?? o.Name); |
|
|
|
var name = type.GetCustomAttribute<DisplayAttribute>()?.Name ?? typeof(TImportModel).Name; |
|
|
|
var ws = workbook.Worksheets.FirstOrDefault(); |
|
|
|
for (int i = 1; i < ws.RowsUsed().Count(); i++) |
|
|
|
{ |
|
|
|
var rowIndex = i + 1; |
|
|
|
var row = ws.Row(rowIndex); |
|
|
|
var model = Activator.CreateInstance<TImportModel>(); |
|
|
|
var validationRresults = new List<ValidationResult>(); |
|
|
|
for (int j = 0; j < ws.ColumnsUsed().Count(); j++) |
|
|
|
{ |
|
|
|
var columnIndex = j + 1; |
|
|
|
var cell = row.Cell(columnIndex); |
|
|
@ -165,39 +233,53 @@ public class ClosedXmlExportImportService : IExportImportService |
|
|
|
if (property != null) |
|
|
|
{ |
|
|
|
var propertyType = property.PropertyType; |
|
|
|
if (propertyType.IsEnum) |
|
|
|
{ |
|
|
|
var enumValue = Enum.GetNames(propertyType) |
|
|
|
.Select(o => new KeyValuePair<string, Enum>(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.ToString() != "") |
|
|
|
{ |
|
|
|
if (value.GetText() == "是") |
|
|
|
if (propertyType.IsEnum) |
|
|
|
{ |
|
|
|
property.SetValue(model, true); |
|
|
|
var enumValue = Enum.GetNames(propertyType) |
|
|
|
.Select(o => new KeyValuePair<string, Enum>(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 |
|
|
|
{ |
|
|
|
property.SetValue(model, false); |
|
|
|
var propertyValue = Convert.ChangeType(value.ToString(), propertyType); |
|
|
|
property.SetValue(model, propertyValue); |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
if (value.ToString() != "") |
|
|
|
if (propertyType.IsEnum || propertyType.Name == nameof(Boolean) || propertyType.IsValueType) |
|
|
|
{ |
|
|
|
var propertyValue = Convert.ChangeType(value.ToString(), propertyType); |
|
|
|
property.SetValue(model, propertyValue); |
|
|
|
var isHasRequiredAttribute = property.HasAttribute<RequiredAttribute>(); |
|
|
|
if (isHasRequiredAttribute == true) |
|
|
|
{ |
|
|
|
var requiredAttribute = property.GetCustomAttribute<RequiredAttribute>(); |
|
|
|
var displayName = property.GetCustomAttribute<DisplayAttribute>()?.Name ?? headerName; |
|
|
|
string errorMessage = string.Format(requiredAttribute.ErrorMessage, displayName); |
|
|
|
validationRresults.Add(new ValidationResult(errorMessage, new string[] { "错误" })); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
result.Add(model); |
|
|
|
result.Add(model, validationRresults); |
|
|
|
} |
|
|
|
//
|
|
|
|
return result; |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
@ -233,7 +315,7 @@ public class ClosedXmlExportImportService : IExportImportService |
|
|
|
else |
|
|
|
{ |
|
|
|
SetCellStyle(row.Cell(1).SetValue("失败"), rowIndex, 1, fontColor: XLColor.Red); |
|
|
|
var desc = string.Join("\\n", errors.Select(o => $"{o.MemberNames?.First()} {o.ErrorMessage}")); |
|
|
|
var desc = string.Join("\n", errors.Select(o => $"{o.MemberNames?.First()} {o.ErrorMessage}")); |
|
|
|
SetCellStyle(row.Cell(2).SetValue(desc), rowIndex, 2, fontColor: XLColor.Red); |
|
|
|
} |
|
|
|
} |
|
|
|