代码之家  ›  专栏  ›  技术社区  ›  Daniel

从Excel表中读取给出错误的值

  •  0
  • Daniel  · 技术社区  · 5 年前

    我想看时间表。不幸的是,这不起作用。而不是 "3:53" 在B1,程序读取并显示 0.16 我正在使用 Microsoft.Office.Interop . 我已经尝试更改表中的格式-但没有效果。

    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

    TimeTable

    1 回复  |  直到 5 年前
        1
  •  1
  •   Robert    5 年前

    时间和日期值通常存储在内部完全不同的电子表格和数据库程序中。因此,当您得到原始值时,您永远不会直接得到您看到的值。在您的例子中,您似乎得到了一个时间/日期值:日期是点之前的值,小数位数是24小时的小数。

    0.16 * 24 = 3.84 hours
    

    0.84 * 60 = 50.4 minutes
    

    我假设与预期值的微小差异是因为值0.16是四舍五入值。使用精确的值并执行计算应该可以得到预期的结果。

    推荐文章