Skip to content

Commit 665d2cc

Browse files
authored
Merge pull request #596 from CN-Hang/master
修复可空枚举导入异常
2 parents 422ddf2 + 791cb58 commit 665d2cc

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

src/Magicodes.ExporterAndImporter.Excel/Utility/ImportHelper.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,12 @@ protected virtual void ParseData(ExcelPackage excelPackage)
12511251
SetValue(cell, dataItem, propertyInfo, value);
12521252
continue;
12531253
}
1254+
// 如果是可空枚举,选中空白项传入"",处理成null,不然会出现SetValue会报错
1255+
else if (propertyInfo.PropertyType.IsNullable() && propertyInfo.PropertyType.GetNullableUnderlyingType().IsEnum)
1256+
{
1257+
SetValue(cell,dataItem,propertyInfo,null);
1258+
continue;
1259+
}
12541260
}
12551261

12561262
if (propertyInfo.PropertyType.IsEnum

src/Magicodes.ExporterAndImporter.Tests/ExcelImporter_Tests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,5 +1210,12 @@ public async Task Issue377_Test()
12101210
import.HasError.ShouldBeFalse();
12111211
import.Data.Count.ShouldBe(3);
12121212
}
1213+
[Fact(DisplayName = "可空枚举类型导入测试")]
1214+
public async Task NullableEnum_Test()
1215+
{
1216+
var filePath = Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "Import", "NullableEnum.xlsx");
1217+
var import = await Importer.Import<NullableEnumTestImportDto>(filePath);
1218+
import.HasError.ShouldBeFalse();
1219+
}
12131220
}
12141221
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
using Magicodes.ExporterAndImporter.Core;
8+
9+
namespace Magicodes.IE.Tests.Models.Import;
10+
/// <summary>
11+
/// 可空枚举问题测试
12+
/// </summary>
13+
public class NullableEnumTestImportDto
14+
{
15+
/// <summary>
16+
/// 编号
17+
/// </summary>
18+
[ImporterHeader(Name = "编号")]
19+
public string No { get; set; }
20+
/// <summary>
21+
/// 等级
22+
/// </summary>
23+
[ImporterHeader(Name = "等级")]
24+
public EnumTest? Level { get; set; }
25+
}
26+
public enum EnumTest
27+
{
28+
A,
29+
B,
30+
}
Binary file not shown.

0 commit comments

Comments
 (0)