我试图将Excel文件读取到VB.NET应用程序。在读取Excel文件时,文件中的列时间不返回时间格式,而是返回一个整数,例如:
17:14:04 --> 0.718101852
13:26:12 --> 0.559861111
7:26:29 --> 0.31005787
如何将这些数字转回时间格式?
请帮助我谢谢
答案 0 :(得分:2)
Dim dttm As DateTime = (New DateTime()).AddDays(worksheet.Cells(2, 3))
TextBox1.Text = ddtm.toString()
答案 1 :(得分:2)
试试这样:
Dim dt As DateTime = (New DateTime()).AddDays(0.559861111)
这对我有用。它返回:
13:26:12 from 0.559861111
答案 2 :(得分:1)
我的Excel工作表是这样的:
我的代码是这样的,它运作良好。
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim DateVal As String, TimeVal As Double, myTime as Date
On Error GoTo OpenCSVfileError
xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Open(FileName)
xlWorkSheet = xlWorkBook.ActiveSheet
Dim xRng As Excel.Range
xRng = CType(xlWorkSheet.Cells(2, 1), Excel.Range)
DateVal = xRng.Value.ToString
myTime = CDate(DateVal)
xRng = CType(xlWorkSheet.Cells(2, 2), Excel.Range)
TimeVal = CDbl(xRng.Value.ToString)
myTime = myTime.AddDays(TimeVal)
OpenCSVfileError:
xlWorkBook.Close()
xlApp.Quit()