包含日期时间和特定区域性信息的C#对象与Excel日期时间单元格完全兼容 [英] C# Object containing datetime and specific cultureinfo to be fully compatible with Excel datetime cell

查看:110
本文介绍了包含日期时间和特定区域性信息的C#对象与Excel日期时间单元格完全兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试将excel文件导入C#对象时,我注意到到目前为止,创建包括特定区域性信息和特定单元格的datetimeformat的dateTime类型的唯一方法是转换为字符串。

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.

例如,可以实例化:

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

但是我还没有看到可以同时包含日期时间值和特定的cultureInfo的任何c#对象。 Excel单元格可能包含的datetimeformat。

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.

请让我知道c#中是否有任何等效类型可以包含dateTime和特定的cultureinfo(包括datetimeFormat)。否则,我需要考虑采用不同的方法。

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.

推荐答案

.NET中没有同时嵌入DateTime和CultureInfo的类型。 。但是您可以自己做:

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; }
}

但是,当您从Excel库中获取日期时,这种类型可能不会有太大帮助,因为Excel不(排他性地)依靠CultureInfo呈现其单元格。 Excel在内部使用其自己的一种数字格式字符串,如下所述:

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/zh-cn/article/available-number-formats-in -excel-0afe8f52-97db-41f1-b972-4b46e9f1e8d2

从本质上讲,.NET支持自己的数字格式字符串,类似于但与Excel的数字格式字符串不兼容:

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/zh-cn/dotnet/standard/base-types/custom-numeric-format-strings

通常,您需要三个单独的信息来显示值:原始值,数字格式字符串和CultureInfo。

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

CultureInfo存储基本内容,例如小数点分隔符,千位分隔符r,货币符号,默认的长/短日期格式以及其他特定于语言环境的内容,但从技术上讲,它无法控制该值在屏幕上的显示方式。

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.

CultureInfo通常是对于您的应用程序是全局的,f.ex是从配置或操作系统中获取的,并且总体上指示应用程序运行的语言/国家。您很少会想将CultureInfo与每个DateTime实例捆绑在一起。

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.

使用哪种数字格式字符串可能取决于用例:有时您可能希望显示短日期(例如列表中的 01-01-2020),有时您可能想要显示显示同一日期的较长版本(例如 1。

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).

其他时候,您可能希望完全像Excel那样呈现电子表格。在这种情况下,同时处理原始单元格值是有意义的和数字格式字符串作为单一类型,例如:

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提供了可以随意使用日期/值的各个部分,但是您必须自己编写代码以将它们粘合在一起,具体取决于用例,使用情况可能会很多。

.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.

这篇关于包含日期时间和特定区域性信息的C#对象与Excel日期时间单元格完全兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆