Skip to content

Commit

Permalink
Support writing DateOnly type to Excel
Browse files Browse the repository at this point in the history
  • Loading branch information
Jef Van den Brandt authored and Jef Van den Brandt committed Mar 26, 2024
1 parent c245d15 commit 2ce7f52
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/MiniExcel/MiniExcelLibs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,9 @@ Todo : https://github.com/mini-software/MiniExcel/projects/1?fullscreen=true</De
<ItemGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Portable.System.DateTimeOnly">
<Version>8.0.0</Version>
</PackageReference>
</ItemGroup>
</Project>
23 changes: 23 additions & 0 deletions src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,29 @@ private Tuple<string, string, string> GetCellValue(int rowIndex, int cellIndex,
cellValue = ((DateTime)value).ToString(columnInfo.ExcelFormat, _configuration.Culture);
}
}
#if NETSTANDARD2_0_OR_GREATER
else if (type == typeof(DateOnly))
{
if (_configuration.Culture != CultureInfo.InvariantCulture)
{
dataType = "str";
cellValue = ((DateOnly)value).ToString(_configuration.Culture);
}
else if (columnInfo == null || columnInfo.ExcelFormat == null)
{
var day = (DateOnly)value;
dataType = "n";
styleIndex = "3";
cellValue = day.ToDateTime(TimeOnly.MinValue).ToOADate().ToString(CultureInfo.InvariantCulture);
}
else
{
// TODO: now it'll lose date type information
dataType = "str";
cellValue = ((DateOnly)value).ToString(columnInfo.ExcelFormat, _configuration.Culture);
}
}
#endif
else
{
//TODO: _configuration.Culture
Expand Down

0 comments on commit 2ce7f52

Please sign in to comment.