From 2ce7f52e454df14359ca64d963ed6f1da28aec9e Mon Sep 17 00:00:00 2001 From: Jef Van den Brandt Date: Tue, 26 Mar 2024 13:17:20 +0100 Subject: [PATCH] Support writing DateOnly type to Excel --- src/MiniExcel/MiniExcelLibs.csproj | 5 ++++ .../OpenXml/ExcelOpenXmlSheetWriter.cs | 23 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/MiniExcel/MiniExcelLibs.csproj b/src/MiniExcel/MiniExcelLibs.csproj index 56ae2a5d..5b1c3733 100644 --- a/src/MiniExcel/MiniExcelLibs.csproj +++ b/src/MiniExcel/MiniExcelLibs.csproj @@ -54,4 +54,9 @@ Todo : https://github.com/mini-software/MiniExcel/projects/1?fullscreen=true + + + 8.0.0 + + diff --git a/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs b/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs index 7c595d50..2552dc27 100644 --- a/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs +++ b/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs @@ -574,6 +574,29 @@ private Tuple 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