Skip to content

Commit

Permalink
dotnetcore#549-Excel中缺少列时,应返回空和错误,不应该空指针异常
Browse files Browse the repository at this point in the history
  • Loading branch information
Farb committed Jan 25, 2024
1 parent 69da777 commit 4258d1d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Magicodes.ExporterAndImporter.Core/Models/ImportResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class ImportResultEnumer<T> where T : class
/// </summary>
public ImportResultEnumer()
{
Data = new List<T>();
RowErrors = new List<DataRowErrorInfo>();
}

Expand Down Expand Up @@ -70,6 +71,7 @@ public class ImportResult<T> where T : class
/// </summary>
public ImportResult()
{
Data = new List<T>();
RowErrors = new List<DataRowErrorInfo>();
}

Expand Down
10 changes: 10 additions & 0 deletions src/Magicodes.ExporterAndImporter.Tests/ExcelImporter_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1210,5 +1210,15 @@ public async Task Issue377_Test()
import.HasError.ShouldBeFalse();
import.Data.Count.ShouldBe(3);
}

[Fact(DisplayName = "#549-Excel中缺少列时,应返回空和错误,不应该空指针异常")]
public async Task MissingField_Test()
{
var filePath = Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "Import", "Issue549.xlsx");
var import = await Importer.Import<Issue549>(filePath);

import.HasError.ShouldBeTrue();
import.Data.Count.ShouldBe(0);
}
}
}
15 changes: 15 additions & 0 deletions src/Magicodes.ExporterAndImporter.Tests/Models/Import/Issue549.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Magicodes.ExporterAndImporter.Core;

namespace Magicodes.IE.Tests.Models.Import;

public class Issue549
{
[ImporterHeader(Name = "姓名", AutoTrim = true)]
public string Name { get; set; }

[ImporterHeader(Name = "年龄", AutoTrim = true)]
public decimal Age { get; set; }

[ImporterHeader(Name = "身高", AutoTrim = true)]
public decimal Height { get; set; }
}
Binary file not shown.

0 comments on commit 4258d1d

Please # to comment.