mirror of
https://gitee.com/dotnetchina/MiniExcel.git
synced 2024-11-29 18:38:08 +08:00
Merge pull request #333 from 0xced/use-configured-culture
Use the configured culture when writing cell values
This commit is contained in:
commit
1f24c671cb
@ -229,43 +229,20 @@ namespace MiniExcelLibs.Csv
|
||||
if (value == null)
|
||||
return "";
|
||||
|
||||
Type type = null;
|
||||
if (p == null)
|
||||
if (value is DateTime dateTime)
|
||||
{
|
||||
type = value.GetType();
|
||||
type = Nullable.GetUnderlyingType(type) ?? type;
|
||||
if (p?.ExcelFormat != null)
|
||||
{
|
||||
return dateTime.ToString(p.ExcelFormat, _configuration.Culture);
|
||||
}
|
||||
else
|
||||
return _configuration.Culture.Equals(CultureInfo.InvariantCulture) ? dateTime.ToString("yyyy-MM-dd HH:mm:ss", _configuration.Culture) : dateTime.ToString(_configuration.Culture);
|
||||
}
|
||||
if (p?.ExcelFormat != null && value is IFormattable formattableValue)
|
||||
{
|
||||
type = p.ExcludeNullableType; //sometime it doesn't need to re-get type like prop
|
||||
return formattableValue.ToString(p.ExcelFormat, _configuration.Culture);
|
||||
}
|
||||
|
||||
|
||||
if (p?.ExcelFormat != null && p?.ExcelFormatToStringMethod != null)
|
||||
{
|
||||
return p.ExcelFormatToStringMethod.Invoke(value, new[] { p.ExcelFormat })?.ToString();
|
||||
}
|
||||
else if (p?.ExcelcultureToStringMethod != null && _configuration.Culture != CultureInfo.InvariantCulture)
|
||||
{
|
||||
return p.ExcelcultureToStringMethod.Invoke(value, new[] { _configuration.Culture })?.ToString();
|
||||
}
|
||||
else if (type == typeof(DateTime))
|
||||
{
|
||||
if (_configuration.Culture != CultureInfo.InvariantCulture)
|
||||
{
|
||||
return ((DateTime)value).ToString(_configuration.Culture);
|
||||
}
|
||||
else if (p == null || p.ExcelFormat == null)
|
||||
{
|
||||
return ((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss");
|
||||
}
|
||||
else
|
||||
{
|
||||
return ((DateTime)value).ToString(p.ExcelFormat);
|
||||
}
|
||||
}
|
||||
|
||||
return value.ToString();
|
||||
return Convert.ToString(value, _configuration.Culture);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -357,14 +357,14 @@ namespace MiniExcelLibs.OpenXml
|
||||
{
|
||||
v = "";
|
||||
}
|
||||
else if (value is string)
|
||||
else if (value is string str)
|
||||
{
|
||||
v = ExcelOpenXmlUtils.EncodeXML(value.ToString());
|
||||
v = ExcelOpenXmlUtils.EncodeXML(str);
|
||||
}
|
||||
else if(p?.ExcelFormat != null && p?.ExcelFormatToStringMethod != null)
|
||||
else if(p?.ExcelFormat != null && value is IFormattable formattableValue)
|
||||
{
|
||||
var formatedStr = p.ExcelFormatToStringMethod.Invoke(value, new[] { p.ExcelFormat })?.ToString();
|
||||
v = ExcelOpenXmlUtils.EncodeXML(formatedStr);
|
||||
var formattedStr = formattableValue.ToString(p.ExcelFormat, _configuration.Culture);
|
||||
v = ExcelOpenXmlUtils.EncodeXML(formattedStr);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -5,7 +5,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Dynamic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
@ -19,9 +18,6 @@
|
||||
public bool Nullable { get; internal set; }
|
||||
public string ExcelFormat { get; internal set; }
|
||||
public double? ExcelColumnWidth { get; internal set; }
|
||||
|
||||
public MethodInfo ExcelFormatToStringMethod { get; internal set; }
|
||||
public MethodInfo ExcelcultureToStringMethod { get; internal set; }
|
||||
}
|
||||
|
||||
internal static partial class CustomPropertyHelper
|
||||
@ -155,15 +151,8 @@
|
||||
var excelColumnName = p.GetAttribute<ExcelColumnNameAttribute>() ;
|
||||
var excludeNullableType = gt ?? p.PropertyType;
|
||||
var excelFormat = p.GetAttribute<ExcelFormatAttribute>()?.Format;
|
||||
MethodInfo formatToStringMethod = null;
|
||||
if(excelFormat != null && excludeNullableType != null)
|
||||
{
|
||||
formatToStringMethod = excludeNullableType.GetMethod("ToString", new[] { typeof(string) });
|
||||
}
|
||||
MethodInfo cultureToStringMethod = excludeNullableType.GetMethod("ToString", new[] { typeof(CultureInfo) });
|
||||
return new ExcelCustomPropertyInfo
|
||||
{
|
||||
|
||||
Property = p,
|
||||
ExcludeNullableType = excludeNullableType,
|
||||
Nullable = gt != null,
|
||||
@ -172,8 +161,6 @@
|
||||
ExcelColumnIndex = p.GetAttribute<ExcelColumnIndexAttribute>()?.ExcelColumnIndex,
|
||||
ExcelColumnWidth = p.GetAttribute<ExcelColumnWidthAttribute>()?.ExcelColumnWidth,
|
||||
ExcelFormat = excelFormat,
|
||||
ExcelFormatToStringMethod = formatToStringMethod,
|
||||
ExcelcultureToStringMethod = cultureToStringMethod
|
||||
};
|
||||
});
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user