简体   繁体   English

从 Excel 表读取给出错误值

[英]Reading from Excel table gives wrong value

I want to read from a TimeTable.我想从时间表中读取。 Unfortunately that doesn't work.不幸的是,这不起作用。 Instead of "3:53" at B1, the program reads and shows 0.16 .程序读取并显示0.16而不是 B1 处的"3:53" How can I improve this?我该如何改进呢? I am using Microsoft.Office.Interop .我正在使用Microsoft.Office.Interop I have already tried to change the formatting in the table - to no avail.我已经尝试更改表格中的格式 - 无济于事。

Private xlApp As Excel.Application = New Excel.Application
Private xlWorkBook As Excel.Workbook
'in a Sub:
xlWorkBook = xlApp.Workbooks.Open(filepath)
 Dim xlWorkSheet As Excel.Worksheet = CType(xlWorkBook.Worksheets("L7"), Excel.Worksheet)
        Dim xlRange As Excel.Range = xlWorkSheet.UsedRange
        Dim ER As Excel.Range
        Dim ER2 As Excel.Range
        Dim Index As Integer = 0
        For Zeile As Integer = 1 To xlRange.Rows.Count
            ER = CType(xlRange.Cells(Zeile, 1), Excel.Range)
            ER2 = CType(xlRange.Cells(Zeile, 2), Excel.Range)
            If CStr(ER2.Value) = "..." Then
                Continue For
            End If
        Dim gesplittet As String() = CStr(ER2.Value).Split(":"c)
'...

ER.Value becomes 0.16 instead of "3:53". ER.Value变为 0.16 而不是“3:53”。 I want to have a String.我想要一个字符串。

时间表

Time and date values are usually stored internally totally different in spread sheet and database programs.时间和日期值通常在内部存储在电子表格和数据库程序中完全不同。 Therefore you will never get directly the values you see as you get the raw values.因此,当您获得原始值时,您永远不会直接获得您看到的值。 In your case it seems like you are getting a time/date value: the date is the value before the dot and the fractional digits are the fraction of the 24 hour day.在您的情况下,您似乎获得了时间/日期值:日期是点之前的值,小数位是 24 小时日的分数。

0.16 * 24 = 3.84 hours

Which means 3 hours and 0.84 of one hour:这意味着 3 小时和 0.84 小时:

0.84 * 60 = 50.4 minutes

I assume the small difference to your expected value is because the value 0.16 was a rounded value.我认为与您的预期值的微小差异是因为值 0.16 是一个四舍五入的值。 Using the exact value and performing the calculations should get you the expected result.使用准确的值并执行计算应该可以得到预期的结果。

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

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