2014-09-26 28 views
0

我正在從VB上收集Arduino的數據並打印。同時我正在寫入excel表格和繪製圖表。我在寫入Excel表格時遇到了問題。被正確dispayed數據,但在寫入到Excel comine兩串 這裏是我的代碼在VB上寫的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 

OUput excel sheet

如果我使用它更改爲

headerText = "Date,TIME ,current, " 
Dim x As String = date1 + "," + time1 + "," + distance 

寫入Excel工作表正常。但是,如果我添加另一個參數瓦特,寫圖像不正確

+1

問題在哪裏? – Psychemaster 2014-09-26 09:06:49

+0

請澄清問題是什麼,你需要什麼幫助,因爲它代表你的問題不清楚 – 2014-09-26 09:08:55

+0

@Psychemaster正如我所說的,如果添加** Dim x As String = date1 +「,」+ time1 +「,」+ distance +「,」+ watt1 **數據打印在excel表格中。否則它打印正確。我需要第三個數據打印 – user50949 2014-09-26 09:26:38

回答

0

你正在寫一個CSV,而不是excel。 Excel只是您決定查看CSV的方法。不要覺得不好,這是一個普遍的錯誤。

請在NOTEPAD中打開CSV,看看它看起來是否正確。如果是這樣,你需要寫一個excel文件來獲得你期望的結果,而不是CSV。

您應該仔細研究使用OLEDBINTEROP,這需要更多的工作,但是您對EXCEL有更多(OLEDB)或完整(INTEROP)控制。