-
Jan 24th, 2021, 04:45 AM
#1
Thread Starter
New Member
Read time from excel sheet
2019 VB
My program opens an excel sheet and adds the data in the cell in a textbox
However, two of the columns consists of Date and the other Time.
The Date cell shows OK but the cell containing time should show 16:00:05
but instead it shows 0.666728618101852
Can anyone show a complete beginner how to correct this
Thanks in advance
Code:
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
Dim APP As New Excel.Application
Dim worksheet As Excel.Worksheet
Dim workbook As Excel.Workbook
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
workbook = APP.Workbooks.Open("C:\Users\user\Desktop\2021-01-19.xlsx")
worksheet = workbook.Worksheets("sheet1")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
TextBox1.Text = worksheet.Cells(2, 1).Value
TextBox2.Text = worksheet.Cells(2, 2).Value
TextBox3.Text = worksheet.Cells(2, 3).Value
TextBox4.Text = worksheet.Cells(2, 4).Value
TextBox5.Text = worksheet.Cells(2, 5).Value
End Sub
Private Sub Form1_FormClosed(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.FormClosedEventArgs) _
Handles MyBase.FormClosed
'workbook.Save()
workbook.Close()
APP.Quit()
End Sub
End Class
-
Jan 24th, 2021, 05:34 AM
#2
Re: Read time from excel sheet
try it this way
Code:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim Path As String = "E:\Test.xlsx"
Dim Excel As ApplicationClass = New ApplicationClass
Dim WorkBook As Workbook = Excel.Workbooks.Open(Path)
Excel.Visible = False
Dim WorkSheets As Sheets = WorkBook.Sheets
Dim WorkSheet As Worksheet = CType(WorkSheets(1), Excel.Worksheet)
'in the cell E7 I have a Time like 18:45:12
Dim d As DateTime = (New DateTime()).AddDays(WorkSheet.Range("E7").Value)
TextBox1.Text = String.Format("{0:HH:mm:ss}", d)
Excel.Quit()
End Sub
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Jan 24th, 2021, 05:47 AM
#3
Thread Starter
New Member
Re: Read time from excel sheet
Wow !
Thanks for that
It works perfectly
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|