diff --git a/ClosedXML/Excel/Cells/XLCell.cs b/ClosedXML/Excel/Cells/XLCell.cs index ca7c91d..d3bae01 100644 --- a/ClosedXML/Excel/Cells/XLCell.cs +++ b/ClosedXML/Excel/Cells/XLCell.cs @@ -223,21 +223,9 @@ // For SetValue we set the cell value directly to the parameter // as opposed to the other SetValue(object value) where we parse the string and try to decude the value - if (value is String || value is char || value is Guid) - { - parsedValue = value.ToInvariantString(); - _dataType = XLDataType.Text; - if (parsedValue.Contains(Environment.NewLine) && !style.Alignment.WrapText) - Style.Alignment.WrapText = true; - - parsed = true; - } - else - { - var tuple = SetKnownTypedValue(value, style); - parsedValue = tuple.Item1; - parsed = tuple.Item2; - } + var tuple = SetKnownTypedValue(value, style, acceptString: true); + parsedValue = tuple.Item1; + parsed = tuple.Item2; // If parsing was unsuccessful, we throw an ArgumentException // because we are using SetValue (typed). @@ -253,11 +241,20 @@ } // TODO: Replace with (string, bool) ValueTuple later - private Tuple SetKnownTypedValue(T value, XLStyleValue style) + private Tuple SetKnownTypedValue(T value, XLStyleValue style, Boolean acceptString) { string parsedValue; bool parsed; - if (value is DateTime d && d >= BaseDate) + if (value is String && acceptString || value is char || value is Guid) + { + parsedValue = value.ToInvariantString(); + _dataType = XLDataType.Text; + if (parsedValue.Contains(Environment.NewLine) && !style.Alignment.WrapText) + Style.Alignment.WrapText = true; + + parsed = true; + } + else if (value is DateTime d && d >= BaseDate) { parsedValue = d.ToOADate().ToInvariantString(); parsed = true; @@ -1579,7 +1576,6 @@ value = default; return false; } - if (currValue == null) { @@ -2095,7 +2091,8 @@ } else { - var tuple = SetKnownTypedValue(value, style); + // Don't accept strings, because we're going to try to parse them later + var tuple = SetKnownTypedValue(value, style, acceptString: false); parsedValue = tuple.Item1; parsed = tuple.Item2; }