C# Excel Reader 将时间戳转换为十进制数
当我使用 Excel Reader 时,除了时间戳之外,它可以正常读取所有内容。例如,它会将 15:59:35 变成 .67290509259259268
我该如何阻止这种情况发生?
object[,] valueArray = (object[,])excelRange.get_Value(XlRangeValueDataType.xlRangeValueDefault);
这是我的数组,它保存从 Excel 工作表中读取的值。不确定是否是这个原因。
When I use the Excel Reader it reads in everything fine except for time stamps. It turns, for example, 15:59:35 into .67290509259259268
How do I stop this from happening?
object[,] valueArray = (object[,])excelRange.get_Value(XlRangeValueDataType.xlRangeValueDefault);
That is my array that is holding the values that are read in from the excel sheet. Not sure if that is the reason.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
DateTime.FromOADate
- 但是,您在问题中提到的数值实际上与您提到的时间并不对应。Try
DateTime.FromOADate
- however, the numeric value you mentioned in the question doesn't actually correspond to the time you mentioned.原因是 Excel 将所有日期时间存储为浮点数。小数部分是时间部分,整数部分代表日期。
您可以使用
Range.Text
值来获取文本,该文本的格式应正确。我认为您不能以与上面相同的方式使用它(我自己尝试做同样的事情,所以还没有得到实际的方法)。另请注意,读取文本可能会很慢(读取数字格式非常慢)。或者尝试使用库,FlexCel 是我们使用的一个非常好的库,或者Apose 以获得更完整的解决方案。
迭代方法(这几乎肯定比返回对象[,]的 get_Value 慢得多)。
(编辑:删除了实际用于 Sharepoint 的其他示例。)
The reason for this is Excel stores all it's DateTimes as floating point numbers. The decimal part is the time component while the integer part represents the date.
You can get use
Range.Text
value to get the text, which should be formatted correctly. I don't think you can use this quite in the same way as above (trying to do the same myself so not got the actual approach yet). Also be wary it might be slow reading the text (reading number formats is v.slow).Alternatively try using a library, FlexCel is a very good one we use, or Apose for a more complete solution.
Itterative Approach (this is almost certainly considerably slower than get_Value returning an object[,]).
(Edit: Removed other examples that were actually for Sharepoint.)