Skip to content

Commit

Permalink
[New] Support DateTimeOffset and ExcelFormat #430 (via @Lightczx , @s…
Browse files Browse the repository at this point in the history
  • Loading branch information
shps951023 committed Sep 18, 2022
1 parent 86aa824 commit 0996ab8
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

### 1.27.0

- [New] Support DateTimeOffset and ExcelFormat #430 (via @Lightczx , @shps951023 )

- [Optimization] SaveAs by datareader support dimension #231 (via @shps951023)

### 1.26.7
Expand Down
2 changes: 2 additions & 0 deletions docs/README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

### 1.27.0

- [New] 支持 DateTimeOffset and ExcelFormat #430 (via @Lightczx , @shps951023 )

- [Optimization] SaveAs by datareader 支持 dimension #231 (via @shps951023)

### 1.26.7
Expand Down
2 changes: 2 additions & 0 deletions docs/README.zh-Hant.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

### 1.27.0

- [New] 支持 DateTimeOffset and ExcelFormat #430 (via @Lightczx , @shps951023 )

- [Optimization] SaveAs by datareader 支持 dimension #231 (via @shps951023)

### 1.26.7
Expand Down
21 changes: 18 additions & 3 deletions src/MiniExcel/Utils/TypeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,21 @@ public static bool IsNumericType(Type type, bool isNullableUnderlyingType = fals
{
newValue = Guid.Parse(itemValue.ToString());
}
else if (pInfo.ExcludeNullableType == typeof(DateTimeOffset))
{
var vs = itemValue?.ToString();
if (pInfo.ExcelFormat != null)
{
if (DateTimeOffset.TryParseExact(vs, pInfo.ExcelFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out var _v))
{
newValue = _v;
}
}
else if (DateTimeOffset.TryParse(vs, _config.Culture, DateTimeStyles.None, out var _v))
newValue = _v;
else
throw new InvalidCastException($"{vs} can't cast to datetime");
}
else if (pInfo.ExcludeNullableType == typeof(DateTime))
{
// fix issue 257 https://github.com/shps951023/MiniExcel/issues/257
Expand All @@ -94,11 +109,11 @@ public static bool IsNumericType(Type type, bool isNullableUnderlyingType = fals
var vs = itemValue?.ToString();
if (pInfo.ExcelFormat != null)
{
if (DateTime.TryParseExact(vs, pInfo.ExcelFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out var _v))
if( pInfo.Property.PropertyType == typeof(DateTimeOffset) && DateTimeOffset.TryParseExact(vs, pInfo.ExcelFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out var _v2))
{
newValue = _v;
newValue = _v2;
}
else if(DateTimeOffset.TryParseExact(vs, pInfo.ExcelFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out var _v))
else if (DateTime.TryParseExact(vs, pInfo.ExcelFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out var _v))
{
newValue = _v;
}
Expand Down
22 changes: 22 additions & 0 deletions tests/MiniExcelTests/MiniExcelIssueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,28 @@ public MiniExcelIssueTests(ITestOutputHelper output)
this.output = output;
}


/// <summary>
/// Exception : MiniExcelLibs.Exceptions.ExcelInvalidCastException: 'ColumnName : Date, CellRow : 2, Value : 2021-01-31 10:03:00 +08:00, it can't cast to DateTimeOffset type.'
/// </summary>
[Fact]
public void TestIssue430()
{
var outputPath = PathHelper.GetTempFilePath();
var value = new[] {
new TestIssue430Dto{ Date=DateTimeOffset.Parse("2021-01-31 10:03:00 +05:00")}
};
MiniExcel.SaveAs(outputPath, value);
var rows = MiniExcel.Query<TestIssue430Dto>(outputPath).ToArray();
Assert.Equal("2021-01-31 10:03:00 +05:00", rows[0].Date.ToString("yyyy-MM-dd HH:mm:ss zzz"));
}

public class TestIssue430Dto
{
[ExcelFormat("yyyy-MM-dd HH:mm:ss zzz")]
public DateTimeOffset Date { get; set; }
}

[Fact]
public void TestIssue_DataReaderSupportDimension()
{
Expand Down

0 comments on commit 0996ab8

Please sign in to comment.