Office Open XML SDK 或第三方中是否有现有的 API 可以正确读取 SpreadsheetML / .xlsx 文件中的日期?
由于有很多变量会影响检测值何时为日期(numFmtId + 自定义数字格式),然后将日期序列转换为 DateTime 值(标准、向后兼容和 1904 超级向后兼容工作簿),因此似乎合乎逻辑的是,这是 SDK 会提供的东西,或者至少有人会有一个现有的代码片段来处理。
我使用的是 C#,但任何语言的解决方案都可以。
Is there an existing API in Office Open XML SDK or a 3rd party to properly read dates from a SpreadsheetML / .xlsx file?
Since there are so many variables that affect detecting when a value is a date (numFmtId + custom number formats) and then converting the date serial to a DateTime value (standard, backwards compatible, and 1904-super-backwards-compatible workbooks), it seems logical that this is something the SDK would provide or at least someone would have an existing code snippet to handle.
I'm using C# but a solution for any language would be fine.
发布评论
评论(2)
看起来还没有任何专门针对此目的的东西。这是我想出的例程。
Looks like there isn't anything out there already specific to this purpose. Here's the routine I came up with.
我以前从未读取过日期,但我想您必须将正在读取的单元格上的样式索引与
x:numFmts
元素中的日期样式索引进行比较,您将在x:cellStyle
中查找。我知道 Office 2010 在单元格上有一个日期数据类型指示器,其中
因此,如果您使用该版本,查找数据是否会更容易是否是一个日期。这就是 Office 2010 中的样子:要将数据转换为 DateTime,我相信您所要做的就是使用
DateTime.FromOADate(cellvalue)
,其中 cellValue 是双精度值。我知道我们在将日期插入 Excel 文档之前将 DateTime 转换为 OADate,因此我想使用 FromOADate 方法会很好。至于执行这些功能的任何 API,我不知道是否有任何 API 可以执行您想要的操作,但我希望它将包含在 SDK 的未来版本中。
I have never read a Date in before, but I'd imagine you would have to compare the style index on the cell you are reading to the date style index in the
x:numFmts
element, which you would find in thex:cellStyle
. I know office 2010 has a date data type indicator on the cell where<x:c t='d'>
so if you are using that version it would be a lot easier to find if the data is a date or not. This is what it would look like in Office 2010:To convert the data into a DateTime I believe all you have to do is a
DateTime.FromOADate(cellvalue)
where cellValue is a double. I know we convert a DateTime to an OADate before inserting dates into our excel documents so I'd imagine using the FromOADate method would work fine.As far as any API's to do these functions, I am not aware of any that will perform what you want, but I wish it would be included in future versions of the SDK.