简体   繁体   English

使用OpenXML从Excel文件中读取日期

[英]Reading Date from Excel File using OpenXML

I'm making a library that uses OpenXML in C# to read excel files. 我正在创建一个在C#中使用OpenXML来读取excel文件的库。 I can read a cell text and numbers just fine, but when it comes to dates there's a problem. 我可以读取细胞文本和数字,但是当涉及到日期时,就会出现问题。 There's the type "date" for the cells, but apparently Excel 2007 doesn't save the dates in that type, so I can't tell if the value I'm reading is a date or not, instead it appears to use styles. 单元格的类型为“日期”,但显然Excel 2007不会保存该类型的日期,因此我无法判断我正在阅读的值是否为日期,而是显示使用样式。

How could I detect if it is a date and return the string representation of it (ex: 29-12-2010)? 我怎么能检测它是否是一个日期并返回它的字符串表示(例如:29-12-2010)?

Excel stores dates as a float value... the integer part being the number of days since 1/1/1900 (or 1/1/1904 depending on which calendar is being used), the fractional part being the proportion of a day (ie the time part)... made slightly more awkward by the fact that 1900 is considered a leap year. Excel将日期存储为浮点值...整数部分是自19/1年1月1日以来的天数(或1/1/1904,具体取决于使用的日历),小数部分是一天的比例(即时间部分... ... 1900被认为是闰年这一事实使得稍微尴尬。

The only thing that differentiates a data from a number is the number format mask. 区分数据和数字的唯一方法是数字格式掩码。 If you can read the format mask, you can use that to identify the value as a date rather than a number... then calculate the date value/formatting from the base date. 如果您可以读取格式掩码,则可以使用它来将值标识为日期而不是数字...然后从基准日期计算日期值/格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM