Skip to content

Commit

Permalink
Merge pull request #349 from YaChengMu/master
Browse files Browse the repository at this point in the history
日期格式化支持DateTimeOffset类型
  • Loading branch information
hueifeng authored Oct 15, 2021
2 parents ca5112d + a9312d2 commit 3790273
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Magicodes.ExporterAndImporter.Core.Extension
{
/// <summary>
///
/// </summary>
public static class ObjectExtension
{
/// <summary>
/// 将 DateTimeOffset 转换成本地 DateTime
/// </summary>
/// <param name="dateTime"></param>
/// <returns></returns>
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;
}

/// <summary>
/// 将 DateTimeOffset? 转换成本地 DateTime?
/// </summary>
/// <param name="dateTime"></param>
/// <returns></returns>
public static DateTime? ConvertToDateTime(this DateTimeOffset? dateTime)
{
return dateTime.HasValue ? (DateTime?)dateTime.Value.ConvertToDateTime() : null;
}
}
}
40 changes: 40 additions & 0 deletions src/Magicodes.ExporterAndImporter.Excel/Utility/ExportHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,46 @@ protected virtual IEnumerable<ExpandoObject> ParseData(ICollection<T> 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<string, object>)obj)[propertyInfo.Name] = mapValue.Key;
}
else
{
((IDictionary<string, object>)obj)[propertyInfo.Name] = value;
}
}
break;
case "Nullable<DateTimeOffset>":
{
var col = ExporterHeaderList.First(a => a.PropertyName == propertyInfo.Name);
var objValue = type.GetProperty(propertyInfo.Name)?.GetValue(dataItem);
if (objValue == null)
{
((IDictionary<string, object>)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<string, object>)obj)[propertyInfo.Name] = mapValue.Key;
}
else
{
((IDictionary<string, object>)obj)[propertyInfo.Name] = dateValue;
}

}
}
break;
default:
((IDictionary<string, object>)obj)[propertyInfo.Name] = type.GetProperty(propertyInfo.Name)
?.GetValue(dataItem);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("加粗文本");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("加粗文本");
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/MagicodesWebSite/ExportTestDataWithAttrs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ public class ExportTestDataWithAttrs

public DateTime Time4 { get; set; }

/// <summary>
/// 时间测试
/// </summary>
[ExporterHeader(DisplayName = "日期5", Format = "yyyy-MM-dd")]
public DateTimeOffset Time5 { get; set; }

/// <summary>
/// 时间测试
/// </summary>
[ExporterHeader(DisplayName = "日期6", Format = "yyyy-MM-dd")]
public DateTimeOffset? Time6 { get { return DateTimeOffset.Now; } }

/// <summary>
/// 长数值测试
/// </summary>
Expand Down

0 comments on commit 3790273

Please # to comment.