[英]Excel sheet writing on VB
I am collecting data from arduino serial and printing on VB . 我正在从arduino串行收集数据并在VB上打印。 At same time i am writing into excel sheet and plotting graph.I am facing problem while writing into excel sheet.
同时我正在写入Excel工作表并绘制图形。我在写入Excel工作表时遇到问题。 data being dispayed properly , But while writing into excel comine two string Here is my code
数据被适当地偿还,但是在写入excel时要用两个字符串这是我的代码
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Static counter As Integer = 0
Static average_sum As Double = 0
Static Avg_count As Integer = 0
Dim voltage As Double = 24
Dim Power As Double
Static Cc_count As Long = 0
Static watt_hour As Double
Try
SerialPort1.Write("c")
System.Threading.Thread.Sleep(250)
Dim k As Double
Dim distance As String = SerialPort1.ReadLine()
k = CDbl(distance)
ListBoxSensor.Text = k
Dim s As New Series
counter = counter + 1
Count_val.Text = counter
Avg_count = Avg_count + 1
' drawing graph
If Avg_count = 1 Then
Power = (distance * voltage) / 3600
watt_hour = watt_hour + Power
'Twatt.Text = watt_hour
Display.Text = watt_hour
voltage_text.Text = 24
System.Threading.Thread.Sleep(250)
If watt_hour > 1000 Then
watt_hour = 1.0 + watt_hour
End If
Avg_count = Avg_count + 1
End If
If Avg_count > 1 Then
Avg_count = 0
End If
Dim current As Double = watt_hour
'Dim current As Double = k
Dim r As DataRow = dT.NewRow
r("time") = Now
'r("current") = k
r("current") = watt_hour
'add the row
dT.Rows.Add(r)
'remove any row older than 1 minute
Dim oldestTime As DateTime = Now.AddMinutes(-1)
Do While DirectCast(dT.Rows(0)("time"), DateTime) < oldestTime
dT.Rows.RemoveAt(0)
Loop
'finally bind the chart....
Chart1.DataBind()
'write into excel sheet for every 1 minute ..................
If Cc_count < 10 Then
Cc_count = Cc_count + 1
Dim array(10) As Double
For value As Double = 0 To Cc_count
array(Cc_count) = distance
average_sum = average_sum + array(Cc_count)
value = value + 1
V_count.Text = value
AVG_count_text.Text = Cc_count
If value > 10 Then
average_sum = average_sum / value
System.Threading.Thread.Sleep(250)
AVG_CR1.Text = average_sum
Cc_count = 0
End If
Next
End If
If counter = 1 Then
counter = 0
Dim headerText = ""
'Dim headerText As New StringBuilder=
Dim csvFile As String = IO.Path.Combine(My.Application.Info.DirectoryPath, "Current.csv")
If Not IO.File.Exists((csvFile)) Then
'headerText = "Date,TIME ,current, "
headerText = "Date-TIME ,current,Watt, "
End If
Using outFile = My.Computer.FileSystem.OpenTextFileWriter(csvFile, True)
If headerText.Length > 0 Then
outFile.WriteLine(headerText)
End If
'Dim date1 As String = "24-10-2014"
'Dim time1 As String = TimeOfDay()
Dim date1 As String = DateAndTime.DateString
Dim watt1 As String = CStr(watt_hour)
' Dim x As String = date1 + "," + time1 + "," + distance + "," + watt1
Dim x As String = date1 + "," + distance + "," + watt1
outFile.Write(x)
'Dim x As String = date1 + "," + time1 + "," + distance
'outFile.Write(x)
End Using
End If
Catch ex As Exception
End Try
End Sub
If i use change it to 如果我使用将其更改为
headerText = "Date,TIME ,current, "
Dim x As String = date1 + "," + time1 + "," + distance
Write into excel sheet properly. 正确写入Excel工作表。 But if i add another perameter watt, Write improperly like in image
但是,如果我再加上一个瓦特瓦,则像图像中那样写不正确
You are writing a CSV, not to excel. 您正在编写CSV,而不是Excel。 Excel is just the method in which you decided to view the CSV.
Excel只是您决定查看CSV的方法。 Don't feel bad, it is a popular mistake.
别难过,这是一个普遍的错误。
Please open the CSV in NOTEPAD and see if it looks correct. 请在NOTEPAD中打开CSV,然后查看它是否正确。 If so, you need to write to an excel file to get the results you expect, instead of to a CSV.
如果是这样,则需要写入excel文件以获得预期的结果,而不是CSV。
You should really look into using OLEDB or INTEROP , which requires more work, but you have more (OLEDB) or complete (INTEROP) control over EXCEL. 您应该真正考虑使用OLEDB或INTEROP ,这需要做更多的工作,但是您对EXCEL拥有更多(OLEDB)或完整(INTEROP)的控制权。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.