C# Object containing datetime and specific cultureinfo to be fully compatible with Excel datetime cell

主宰稳场 提交于 2020-05-17 05:46:13

问题


As I am trying to import excel file into C# object, I notice that so far the only way to create a dateTime type including the specific cultureinfo and the datetimeformat of the specific cell is converting into string.

For example, it can be instantiated:

var date = new DateTime(DateTime.Now.Ticks, DateTimeKind.Unspecified);
var culturedDateString = date.ToString(specifiedCulture);

But I have not seen any c# object that can contain both datetime value as well as the specific cultureInfo and datetimeformat that Excel cell might contain.

Please let me know if there is any equivalent type in c# which can contain both dateTime and specific cultureinfo including datetimeFormat. Otherwise I need to think about taking different approaches.


回答1:


There is no type in .NET which embeds both a DateTime and a CultureInfo. But you can make one yourself:

public class DateTimeAndCultureInfo {
    public DateTime DateTime { get; set; }
    public CultureInfo CultureInfo { get;set; }
}

However, when you get a date from an Excel library, such a type probably won't help much, because Excel doesn't (exclusively) rely on a CultureInfo to render its cells. Internally Excel uses its own kind of number format strings as described here:

https://support.office.com/en-us/article/available-number-formats-in-excel-0afe8f52-97db-41f1-b972-4b46e9f1e8d2

Tangentially, .NET supports its own kind of number format strings, which are similar to, but incompatible with Excel's number format strings:

https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings

Usually you need three separate pieces of information to display a value: the raw value, a number format string and a CultureInfo.

The CultureInfo stores basic stuff like decimal separator, thousand separator, currency symbol, default long/short date formats, and other locale-specific stuff, but technically does not control how the value appears on screen.

The CultureInfo is typically global to your application, f.ex taken from a configuration or OS, and indicates overall what language/country the application is running in. Only rarely would you want to bundle a CultureInfo with every DateTime instance.

What number format string to use may depend on the use case: some times you might want to display a short date (like "01-01-2020" in a list), some times you might want to display a longer version of the same date (like "1. january 2020" in an overview).

Other times, you might want to render a spreadsheet exactly like Excel. In these cases it makes sense to treat both the raw cell value and number format string as a single type, f.ex:

public class Cell {
    public object Value { get; set; }
    public string FormatString { get;set; }
}

.NET provides the individual pieces to work with dates/values however you want, but you have to write the code yourself to glue it all together depending on the use case, which can vary plenty.



来源:https://stackoverflow.com/questions/61559823/c-sharp-object-containing-datetime-and-specific-cultureinfo-to-be-fully-compatib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!