From ce5bf1b60caa629f2eb05e99271483f940567c19 Mon Sep 17 00:00:00 2001 From: YaChengMu Date: Fri, 15 Oct 2021 11:49:45 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E7=9B=AE=E5=89=8D=E6=97=A5=E6=9C=9F?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E4=B8=8D=E6=94=AF=E6=8C=81DateTimeO?= =?UTF-8?q?ffset=E7=B1=BB=E5=9E=8B=EF=BC=8C=E4=BF=AE=E6=94=B9=E5=90=8E?= =?UTF-8?q?=E5=AF=BC=E5=87=BADateTimeOffset=E7=B1=BB=E5=9E=8B=E6=97=B6?= =?UTF-8?q?=E4=BC=9A=E5=85=88=E8=BD=AC=E6=8D=A2=E4=B8=BADateTime=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=EF=BC=8C=E5=86=8D=E6=A0=BC=E5=BC=8F=E5=8C=96=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Extension/ObjectExtension.cs | 37 +++++++++++++++++ .../Utility/ExportHelper.cs | 40 +++++++++++++++++++ .../ExportTestDataWithAttrs.cs | 12 ++++++ 3 files changed, 89 insertions(+) create mode 100644 src/Magicodes.ExporterAndImporter.Core/Extension/ObjectExtension.cs diff --git a/src/Magicodes.ExporterAndImporter.Core/Extension/ObjectExtension.cs b/src/Magicodes.ExporterAndImporter.Core/Extension/ObjectExtension.cs new file mode 100644 index 00000000..eda35149 --- /dev/null +++ b/src/Magicodes.ExporterAndImporter.Core/Extension/ObjectExtension.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Magicodes.ExporterAndImporter.Core.Extension +{ + /// + /// + /// + public static class ObjectExtension + { + /// + /// 将 DateTimeOffset 转换成本地 DateTime + /// + /// + /// + public static DateTime ConvertToDateTime(this DateTimeOffset dateTime) + { + if (dateTime.Offset.Equals(TimeSpan.Zero)) + return dateTime.UtcDateTime; + if (dateTime.Offset.Equals(TimeZoneInfo.Local.GetUtcOffset(dateTime.DateTime))) + return dateTime.ToLocalTime().DateTime; + else + return dateTime.DateTime; + } + + /// + /// 将 DateTimeOffset? 转换成本地 DateTime? + /// + /// + /// + public static DateTime? ConvertToDateTime(this DateTimeOffset? dateTime) + { + return dateTime.HasValue ? (DateTime?)dateTime.Value.ConvertToDateTime() : null; + } + } +} diff --git a/src/Magicodes.ExporterAndImporter.Excel/Utility/ExportHelper.cs b/src/Magicodes.ExporterAndImporter.Excel/Utility/ExportHelper.cs index 22f6a897..bc1215eb 100644 --- a/src/Magicodes.ExporterAndImporter.Excel/Utility/ExportHelper.cs +++ b/src/Magicodes.ExporterAndImporter.Excel/Utility/ExportHelper.cs @@ -748,6 +748,46 @@ protected virtual IEnumerable ParseData(ICollection dataItems) } } break; + case "DateTimeOffset": + { + var col = ExporterHeaderList.First(a => a.PropertyName == propertyInfo.Name); + var val = type.GetProperty(propertyInfo.Name)?.GetValue(dataItem).ToString(); + DateTime value = DateTimeOffset.Parse(val).ConvertToDateTime(); + if (col.MappingValues.Count > 0 && col.MappingValues.ContainsValue(value)) + { + var mapValue = col.MappingValues.FirstOrDefault(f => f.Value.ToString() == value.ToString()); + ((IDictionary)obj)[propertyInfo.Name] = mapValue.Key; + } + else + { + ((IDictionary)obj)[propertyInfo.Name] = value; + } + } + break; + case "Nullable": + { + var col = ExporterHeaderList.First(a => a.PropertyName == propertyInfo.Name); + var objValue = type.GetProperty(propertyInfo.Name)?.GetValue(dataItem); + if (objValue == null) + { + ((IDictionary)obj)[propertyInfo.Name] = null; + } + else + { + var dateValue = ((DateTimeOffset?)(objValue)).ConvertToDateTime(); + if (dateValue.HasValue && col.MappingValues.Count > 0 && col.MappingValues.ContainsValue(dateValue)) + { + var mapValue = col.MappingValues.FirstOrDefault(f => f.Value == dateValue); + ((IDictionary)obj)[propertyInfo.Name] = mapValue.Key; + } + else + { + ((IDictionary)obj)[propertyInfo.Name] = dateValue; + } + + } + } + break; default: ((IDictionary)obj)[propertyInfo.Name] = type.GetProperty(propertyInfo.Name) ?.GetValue(dataItem); diff --git a/src/MagicodesWebSite/ExportTestDataWithAttrs.cs b/src/MagicodesWebSite/ExportTestDataWithAttrs.cs index 9c0e7232..7bb2574c 100644 --- a/src/MagicodesWebSite/ExportTestDataWithAttrs.cs +++ b/src/MagicodesWebSite/ExportTestDataWithAttrs.cs @@ -35,6 +35,18 @@ public class ExportTestDataWithAttrs public DateTime Time4 { get; set; } + /// + /// 时间测试 + /// + [ExporterHeader(DisplayName = "日期5", Format = "yyyy-MM-dd")] + public DateTimeOffset Time5 { get; set; } + + /// + /// 时间测试 + /// + [ExporterHeader(DisplayName = "日期6", Format = "yyyy-MM-dd")] + public DateTimeOffset? Time6 { get { return DateTimeOffset.Now; } } + /// /// 长数值测试 /// From a9312d2468c2334cf369b88d1a1269591d540b08 Mon Sep 17 00:00:00 2001 From: HueiFeng <695979933@qq.com> Date: Fri, 15 Oct 2021 13:29:48 +0800 Subject: [PATCH 2/2] Update UT --- .../MagicodesFilter_Tests.cs | 2 +- .../MagicodesMiddleware_Tests.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Magicodes.ExporterAndImporter.Tests/MagicodesFilter_Tests.cs b/src/Magicodes.ExporterAndImporter.Tests/MagicodesFilter_Tests.cs index cce568a4..3caf4d8d 100644 --- a/src/Magicodes.ExporterAndImporter.Tests/MagicodesFilter_Tests.cs +++ b/src/Magicodes.ExporterAndImporter.Tests/MagicodesFilter_Tests.cs @@ -137,7 +137,7 @@ public async Task XlsxHttpContentMediaType_AttrsExport_Test() sheet.Tables.Count.ShouldBe(1); var tb = sheet.Tables.First(); - tb.Columns.Count.ShouldBe(9); + tb.Columns.Count.ShouldBe(11); tb.Columns.First().Name.ShouldBe("加粗文本"); } } diff --git a/src/Magicodes.ExporterAndImporter.Tests/MagicodesMiddleware_Tests.cs b/src/Magicodes.ExporterAndImporter.Tests/MagicodesMiddleware_Tests.cs index fcbd5648..c470fb16 100644 --- a/src/Magicodes.ExporterAndImporter.Tests/MagicodesMiddleware_Tests.cs +++ b/src/Magicodes.ExporterAndImporter.Tests/MagicodesMiddleware_Tests.cs @@ -156,7 +156,7 @@ public async Task XlsxHttpContentMediaType_AttrsExport_Test() sheet.Tables.Count.ShouldBe(1); var tb = sheet.Tables.First(); - tb.Columns.Count.ShouldBe(9); + tb.Columns.Count.ShouldBe(11); tb.Columns.First().Name.ShouldBe("加粗文本"); } }