Skip to content
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
Binary file added samples/xlsx/TestIssue869/DateTimeMidnight.xlsx
Binary file not shown.
Binary file not shown.
20 changes: 20 additions & 0 deletions src/MiniExcel.Core/Enums/DateOnlyConversionMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace MiniExcelLib.Core.Enums;


public enum DateOnlyConversionMode
{
/// <summary>
/// No conversion is applied and DateOnly values are not transformed.
/// </summary>
None,

/// <summary>
/// Allows conversion from DateTime to DateOnly only if the time component is exactly midnight (00:00:00).
/// </summary>
RequireMidnight,

/// <summary>
/// Converts DateTime to DateOnly by ignoring the time part completely, assuming the time component is not critical.
/// </summary>
IgnoreTimePart
}
6 changes: 6 additions & 0 deletions src/MiniExcel.Core/MiniExcelConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MiniExcelLib.Core.Attributes;
using MiniExcelLib.Core.Enums;

namespace MiniExcelLib.Core;

Expand All @@ -15,4 +16,9 @@ public abstract class MiniExcelBaseConfiguration : IMiniExcelConfiguration
/// When exporting using DataReader, the data not in DynamicColumn will be filtered.
/// </summary>
public bool DynamicColumnFirst { get; set; } = false;

/// <summary>
/// Specifies when and how DateTime values are converted to DateOnly values.
/// </summary>
public DateOnlyConversionMode DateOnlyConversionMode { get; set; }
}
9 changes: 9 additions & 0 deletions src/MiniExcel.Core/Reflection/MiniExcelMapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel;
using MiniExcelLib.Core.Enums;
using MiniExcelLib.Core.Exceptions;

namespace MiniExcelLib.Core.Reflection;
Expand Down Expand Up @@ -27,7 +28,7 @@
headersDic = CustomPropertyHelper.GetHeaders(item, trimColumnNames);

//TODO: alert don't duplicate column name
props = CustomPropertyHelper.GetExcelCustomPropertyInfos(type, keys, configuration);

Check warning on line 31 in src/MiniExcel.Core/Reflection/MiniExcelMapper.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Nullability of reference types in value of type 'List<MiniExcelColumnInfo?>' doesn't match target type 'List<MiniExcelColumnInfo>'.
first = false;

// if we treat the header as data we move forwards with the mapping otherwise we jump to the next iteration
Expand Down Expand Up @@ -150,6 +151,14 @@
return newValue;
}

if (itemValue is DateTime dateTimeValue && config.DateOnlyConversionMode is not DateOnlyConversionMode.None)
{
if (config.DateOnlyConversionMode == DateOnlyConversionMode.RequireMidnight && dateTimeValue.TimeOfDay != TimeSpan.Zero)
throw new InvalidCastException($"Could not convert cell of type DateTime to DateOnly, because DateTime was not at midnight, but at {dateTimeValue:HH:mm:ss}.");

return DateOnly.FromDateTime(dateTimeValue);
}

var vs = itemValue?.ToString();
if (pInfo.ExcelFormat is not null)
{
Expand Down
42 changes: 39 additions & 3 deletions tests/MiniExcel.Core.Tests/MiniExcelIssueTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.ComponentModel;
using System.Text.RegularExpressions;
using MiniExcelLib.Core.Enums;
using MiniExcelLib.Core.Exceptions;
using MiniExcelLib.Core.OpenXml.Picture;
using MiniExcelLib.Core.OpenXml.Utils;
Expand All @@ -16,9 +17,7 @@ public class MiniExcelIssueTests(ITestOutputHelper output)
private readonly OpenXmlExporter _excelExporter = MiniExcel.Exporters.GetOpenXmlExporter();
private readonly OpenXmlTemplater _excelTemplater = MiniExcel.Templaters.GetOpenXmlTemplater();

// private readonly OpenXmlImporter _csvImporter = MiniExcel.Importer.GetCsvImporter();
// private readonly OpenXmlExporter _csvExporter = MiniExcel.Exporter.GetCsvExporter();


/// <summary>
/// https://github.com/mini-software/MiniExcel/issues/549
/// </summary>
Expand Down Expand Up @@ -3653,4 +3652,41 @@ public void TestIssue809()
Assert.Equal(null, rows[0].A);
Assert.Equal(2, rows[2].B);
}


private class Issue869
{
public string? Name { get; set; }
public DateOnly? Date { get; set; }
}

[Theory]
[InlineData("DateTimeMidnight", DateOnlyConversionMode.None, true)]
[InlineData("DateTimeNotMidnight", DateOnlyConversionMode.None, true)]
[InlineData("DateTimeMidnight", DateOnlyConversionMode.RequireMidnight, false)]
[InlineData("DateTimeNotMidnight", DateOnlyConversionMode.RequireMidnight, true)]
[InlineData("DateTimeMidnight", DateOnlyConversionMode.IgnoreTimePart, false)]
[InlineData("DateTimeNotMidnight", DateOnlyConversionMode.IgnoreTimePart, false)]
public void TestIssue869(string fileName, DateOnlyConversionMode mode, bool throwsException)
{
var path = PathHelper.GetFile($"xlsx/TestIssue869/{fileName}.xlsx");
var config = new OpenXmlConfiguration { DateOnlyConversionMode = mode };

var testFn = () => _excelImporter.Query<Issue869>(path, configuration: config).ToList();
if (throwsException)
{
Assert.Throws<MiniExcelInvalidCastException>(testFn);
}
else
{
try
{
_ = testFn();
}
catch (Exception ex)
{
Assert.Fail($"No exception should be thrown, but one was still thrown: {ex}.");
}
}
}
}
Loading