Skip to content

Commit 0996ab8

Browse files
committed
[New] Support DateTimeOffset and ExcelFormat mini-software#430 (via @Lightczx , @shps951023 )
1 parent 86aa824 commit 0996ab8

5 files changed

Lines changed: 46 additions & 3 deletions

File tree

docs/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
### 1.27.0
2828

29+
- [New] Support DateTimeOffset and ExcelFormat #430 (via @Lightczx , @shps951023 )
30+
2931
- [Optimization] SaveAs by datareader support dimension #231 (via @shps951023)
3032

3133
### 1.26.7

docs/README.zh-CN.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
### 1.27.0
3131

32+
- [New] 支持 DateTimeOffset and ExcelFormat #430 (via @Lightczx , @shps951023 )
33+
3234
- [Optimization] SaveAs by datareader 支持 dimension #231 (via @shps951023)
3335

3436
### 1.26.7

docs/README.zh-Hant.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
### 1.27.0
3030

31+
- [New] 支持 DateTimeOffset and ExcelFormat #430 (via @Lightczx , @shps951023 )
32+
3133
- [Optimization] SaveAs by datareader 支持 dimension #231 (via @shps951023)
3234

3335
### 1.26.7

src/MiniExcel/Utils/TypeHelper.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,21 @@ public static bool IsNumericType(Type type, bool isNullableUnderlyingType = fals
8181
{
8282
newValue = Guid.Parse(itemValue.ToString());
8383
}
84+
else if (pInfo.ExcludeNullableType == typeof(DateTimeOffset))
85+
{
86+
var vs = itemValue?.ToString();
87+
if (pInfo.ExcelFormat != null)
88+
{
89+
if (DateTimeOffset.TryParseExact(vs, pInfo.ExcelFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out var _v))
90+
{
91+
newValue = _v;
92+
}
93+
}
94+
else if (DateTimeOffset.TryParse(vs, _config.Culture, DateTimeStyles.None, out var _v))
95+
newValue = _v;
96+
else
97+
throw new InvalidCastException($"{vs} can't cast to datetime");
98+
}
8499
else if (pInfo.ExcludeNullableType == typeof(DateTime))
85100
{
86101
// fix issue 257 https://github.com/shps951023/MiniExcel/issues/257
@@ -94,11 +109,11 @@ public static bool IsNumericType(Type type, bool isNullableUnderlyingType = fals
94109
var vs = itemValue?.ToString();
95110
if (pInfo.ExcelFormat != null)
96111
{
97-
if (DateTime.TryParseExact(vs, pInfo.ExcelFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out var _v))
112+
if( pInfo.Property.PropertyType == typeof(DateTimeOffset) && DateTimeOffset.TryParseExact(vs, pInfo.ExcelFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out var _v2))
98113
{
99-
newValue = _v;
114+
newValue = _v2;
100115
}
101-
else if(DateTimeOffset.TryParseExact(vs, pInfo.ExcelFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out var _v))
116+
else if (DateTime.TryParseExact(vs, pInfo.ExcelFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out var _v))
102117
{
103118
newValue = _v;
104119
}

tests/MiniExcelTests/MiniExcelIssueTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,28 @@ public MiniExcelIssueTests(ITestOutputHelper output)
3434
this.output = output;
3535
}
3636

37+
38+
/// <summary>
39+
/// Exception : MiniExcelLibs.Exceptions.ExcelInvalidCastException: 'ColumnName : Date, CellRow : 2, Value : 2021-01-31 10:03:00 +08:00, it can't cast to DateTimeOffset type.'
40+
/// </summary>
41+
[Fact]
42+
public void TestIssue430()
43+
{
44+
var outputPath = PathHelper.GetTempFilePath();
45+
var value = new[] {
46+
new TestIssue430Dto{ Date=DateTimeOffset.Parse("2021-01-31 10:03:00 +05:00")}
47+
};
48+
MiniExcel.SaveAs(outputPath, value);
49+
var rows = MiniExcel.Query<TestIssue430Dto>(outputPath).ToArray();
50+
Assert.Equal("2021-01-31 10:03:00 +05:00", rows[0].Date.ToString("yyyy-MM-dd HH:mm:ss zzz"));
51+
}
52+
53+
public class TestIssue430Dto
54+
{
55+
[ExcelFormat("yyyy-MM-dd HH:mm:ss zzz")]
56+
public DateTimeOffset Date { get; set; }
57+
}
58+
3759
[Fact]
3860
public void TestIssue_DataReaderSupportDimension()
3961
{

0 commit comments

Comments
 (0)