Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

日期格式化支持DateTimeOffset类型 #349

Merged
merged 3 commits into from
Oct 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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