From 0996ab8032648e067c7d4288bf632dba3c95e2b8 Mon Sep 17 00:00:00 2001 From: WeiLin Date: Sun, 18 Sep 2022 23:24:46 +0800 Subject: [PATCH] [New] Support DateTimeOffset and ExcelFormat #430 (via @Lightczx , @shps951023 ) --- docs/README.md | 2 ++ docs/README.zh-CN.md | 2 ++ docs/README.zh-Hant.md | 2 ++ src/MiniExcel/Utils/TypeHelper.cs | 21 +++++++++++++++++--- tests/MiniExcelTests/MiniExcelIssueTests.cs | 22 +++++++++++++++++++++ 5 files changed, 46 insertions(+), 3 deletions(-) diff --git a/docs/README.md b/docs/README.md index 0a33ee72..c4f52f10 100644 --- a/docs/README.md +++ b/docs/README.md @@ -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 diff --git a/docs/README.zh-CN.md b/docs/README.zh-CN.md index 9bb1c2e1..44d817cc 100644 --- a/docs/README.zh-CN.md +++ b/docs/README.zh-CN.md @@ -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 diff --git a/docs/README.zh-Hant.md b/docs/README.zh-Hant.md index 942f6867..e93d7efe 100644 --- a/docs/README.zh-Hant.md +++ b/docs/README.zh-Hant.md @@ -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 diff --git a/src/MiniExcel/Utils/TypeHelper.cs b/src/MiniExcel/Utils/TypeHelper.cs index 1c3a9b5c..ff28ddcb 100644 --- a/src/MiniExcel/Utils/TypeHelper.cs +++ b/src/MiniExcel/Utils/TypeHelper.cs @@ -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 @@ -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; } diff --git a/tests/MiniExcelTests/MiniExcelIssueTests.cs b/tests/MiniExcelTests/MiniExcelIssueTests.cs index 8d3d95e4..80408ed7 100644 --- a/tests/MiniExcelTests/MiniExcelIssueTests.cs +++ b/tests/MiniExcelTests/MiniExcelIssueTests.cs @@ -34,6 +34,28 @@ public MiniExcelIssueTests(ITestOutputHelper output) this.output = output; } + + /// + /// Exception : MiniExcelLibs.Exceptions.ExcelInvalidCastException: 'ColumnName : Date, CellRow : 2, Value : 2021-01-31 10:03:00 +08:00, it can't cast to DateTimeOffset type.' + /// + [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(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() {