如何解释 Excel 数字格式字符串以确定是否应由 DateTime.FromOADate 解析该值
如何创建一个函数“bool IsDateTime”,该函数将可靠地确定 Excel 数字格式字符串(如“[$-409]h:mm:ss AM/PM;@”)是否表明该数值是应该传递的日期时间到 DateTime.FromOADate?
我已经弄清楚 [$-409] 是什么: Excel 数字格式:什么是“[$-409]”?。这只是一个区域设置代码。
我还阅读了一些关于数字格式字符串被分号分隔成四个格式部分的信息: http://office.microsoft.com/en-us/excel-help/create-or-delete-a-custom-number-format-HP005199500.aspx?CTT=5&origin=HP005198679这里 http://www.ozgrid.com/Excel/excel-custom-number-formats.htm
例如,简单地搜索日期/时间格式字符(如 h、)的出现是否可靠? m、s、y、d? Excel 会如何解释它?
如果问题不清楚...当您读取 Excel 文件并查看日期/时间值时,您实际上看到的是普通的旧双精度值,因为它就是这样的存储在 Excel 中。要确定它是普通双精度型还是应传递给 DateTime.FromOADate 的双精度型,您必须解释自定义数字格式字符串。所以我问如何解释这样一个字符串(它可能引用也可能不引用日期/时间值),以确定是否应通过 DateTime.FromOADate 将双精度值转换为 DateTime 值。此外,如果成功转换为 DateTime 值,我需要将 Excel 数字格式字符串转换为等效的 .NET DateTime 格式字符串,以便我可以像 Excel 一样通过 DateTime.ToString(converted_format_string ) 显示日期/时间值。
How can I create a function "bool IsDateTime" that will reliably determine whether an Excel number format string like "[$-409]h:mm:ss AM/PM;@" indicates that the numeric value is a DateTime that should be passed to DateTime.FromOADate?
I've figured out what the [$-409] is: Excel Number Format: What is "[$-409]"?. It's just a locale code.
I've also read a little about the number format string being separated into four format sections by semicolons: http://office.microsoft.com/en-us/excel-help/create-or-delete-a-custom-number-format-HP005199500.aspx?CTT=5&origin=HP005198679 and here http://www.ozgrid.com/Excel/excel-custom-number-formats.htm
For example, would it be reliable to simply search for occurrences of the date/time format characters like h,m,s,y,d? How might Excel interpret it?
In case the question is not clear... when you read an Excel file and look at a date/time value, you're actually looking at a plain old double-precision value, because that's how it's stored in Excel. To figure out whether it's an ordinary double or a double that should be passed to DateTime.FromOADate, you must interpret the custom number format string. So I am asking how to go about interpreting such a string, which may or may not refer to a date/time value, in order to determine whether the double-precision value should be converted to a DateTime value via DateTime.FromOADate. Furthermore, if successfully converted to a DateTime value, I would then need to convert the Excel number format string into an equivalent .NET DateTime format string so I could display the date/time value as Excel would via DateTime.ToString( converted_format_string ).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 CELL 函数并返回格式来检查单元格是否包含任何内置日期格式。如果使用内置格式,它将返回“D”后跟一个数字。
例如:
对于更一般的情况,我首先检查单元格是否为数字 (
ISNUMBER()
) 并在日期范围内(即在 0 和TODAY()
之间)代码> - 今天是 39296)。然后,我会检查数字格式中至少出现一个 d、m、y、h、M 或 s,因为这应该表示单元格中有一个日期。希望这有帮助,
戴夫
You can check if the cell contains any of the built in date formats by using the CELL function and returning the format. It will return "D" followed by a number if it is using a built in format.
For example:
For a more general case I would first check is the cell a number (
ISNUMBER()
) and within the range for a date (i.e., between 0 andTODAY()
- which is 39296 today). I would then check the number format for the occurrence of at least one d, m, y, h, M or s as this should signify that you have a date in the cell.Hope this helps,
Dave
我实现了一个类来解析 Excel 数字格式字符串。它查看第一部分(格式字符串中四个可能的部分),并使用正则表达式捕获特定于日期/时间的自定义格式字符,例如“y”、“m”、“d”、“h”、“s” "、"AM/PM",如果没有找到则返回 null。第一步只是确定格式字符串是否适用于日期/时间值,并为我们留下逻辑日期/时间格式说明符的面向对象的有序列表以供进一步处理。
假设确定格式字符串用于日期/时间值,则捕获和分类的值将按照它们在原始格式字符串中找到的顺序进行排序。
接下来,它应用 Excel 特定的格式怪癖,例如确定“m”是否表示月份或分钟,仅当它紧接在“h”之后或“s”之前(它们之间允许文字文本)时才将其解释为“分钟” ,所以它并不完全是“立即”之前/之后)。如果未指定“AM/PM”,Excel 还会强制“h”字符采用 24 小时时间,因此如果未找到“AM/PM”,它将使用小写 m(.NET 中的 24 小时时间),否则它会将其转换为大写 M(.NET 中的 12 小时时间)。它还将“AM/PM”转换为 .NET 等效的“tt”,并空白条件表达式,这些条件表达式不能包含在纯 .NET DateTime 格式字符串中。
上面的类可在以下上下文中使用,将 Excel 文件中具有非空标题的列中的字符串值读取到 DataTable 中。具体来说,它尝试获取有效的 DateTime 实例,如果找到,它会尝试从 Excel 数字格式字符串构造有效的 .NET DateTime 格式字符串。如果前面的两个步骤都成功,则会将格式化的日期时间字符串存储在数据表中,否则会将存在的任何值转换为字符串(确保首先删除富文本格式(如果存在)):
I implemented a class to parse the Excel number format string. It looks at the first section (of four possible sections in the format string), and uses a Regex to capture date/time specific custom format characters such as "y", "m", "d", "h", "s", "AM/PM", and returns null if none are found. This first step simply decides whether the format string is meant for a date/time value, and leaves us with an object-oriented ordered list of logical date/time format specifiers for further processing.
Assuming it was decided that the format string is meant for a date/time value, the captured and classified values are sorted into the order they were found in the original format string.
Next, it applies Excel-specific formatting quirks, like deciding whether "m" means month or minute, interpreting it as "minute" only if it appears immediately after an "h" or before an "s" (literal text is allowed between them, so it's not exactly "immediately" before/after). Excel also forces 24-hour time for the "h" character if "AM/PM" is not also specified, so if "AM/PM" is not found, it uses the lowercase m (24-hour time in .NET), otherwise it converts it to a capital M (12-hour time in .NET). It also converts "AM/PM" to the .NET equivalent "tt", and blanks out conditional expressions, which cannot be included in a plain .NET DateTime format string.
The above class can be used in the following context to read string values into a DataTable from the columns in an Excel file that have non-null headers. Specifically, it attempts to acquire a valid DateTime instance, and if one is found, it attempts to construct a valid .NET DateTime format string from the Excel number format string. If both of the previous steps are successfuly, it stores the formatted date time string in the data table, and otherwise it converts whatever value is present to a string (ensuring to strip out rich text formatting first if present):