如何仅在第一行生成 .csv header?
How do I generate .csv header on first line only?
如何使用 VB.NET 代码仅在第一行生成 .csv header?下面是我试过的代码,但它写在 .csv 文件的顶部和列上。
Try
My.Computer.FileSystem.WriteAllText("C:\bin\Debug\TimerTest.csv",
"Date and Time,Tag Name, Value" & vbCrLf &
timeUtc & "," & Tag_name_Read & "," & itemValues(0).Value.ToString, True)
'Date and Time, Tag Name, Value are my header. they are suppose to be on the first line
Catch ex As Exception
End Try
If daServerMgt.IsConnected Then
daServerMgt.Disconnect()
End If
Update Link Showing the csv file)
我只想在顶部显示 header(日期和时间 | 标签名称 | 值),我不希望它出现在列中。它附加它是因为 My.Computer.FileSystem.WriteAllText(File, Text, true)
我该如何解决这个问题?
除了 @Enigmativity 提到的问题之外,您的代码似乎没有任何问题。
我的旧机器最近崩溃了,我还没有在我的 b运行d 新机器上安装 Visual Studio,但我在 C:\Temp
文件夹中创建了一个文件名为 TimerTest.vb
,内容如下:
Imports System
Module Module1
Public Structure Struct1
Public Value As Integer
Sub New(ByVal _Value As Integer)
Value = _Value
End Sub
End Structure
Sub Main()
Dim timeUtc As DateTime = DateTime.Now()
Dim Tag_name_Read As String = "FortyTwo"
Dim itemValues = New Struct1() { New Struct1(42), New Struct1(43), New Struct1(44)}
Try
'My.Computer.FileSystem.WriteAllText("C:\bin\Debug\TimerTest.csv",
My.Computer.FileSystem.WriteAllText("C:\Temp\TimerTest.csv",
"Date and Time,Tag Name, Value" & vbCrLf &
timeUtc & "," & Tag_name_Read & "," & itemValues(0).Value.ToString, True)
'Date and Time, Tag Name, Value are my header. they are suppose to be on the first line
Catch ex As Exception
End Try
'If daServerMgt.IsConnected Then
' daServerMgt.Disconnect()
'End If
End Sub
End Module
然后我通过 运行 在命令 Window 中编译了以下语句(vbc.exe
的父文件夹已经在我的 %PATH%
中):
vbc C:\Temp\TimerTest.vb
在我运行生成TimerTest.exe
文件后,在C:\Temp
中生成了一个TimerTest.csv
文件,内容如下:
Date and Time,Tag Name, Value<br>
10/27/2016 9:07:31 PM,FortyTwo,42
当我双击 TimerTest.csv
文件时,它在 Excel 中打开没有任何问题:
因此,问题一定出在代码的其他地方。为了让我们更容易地帮助您,请提供一个 Minimal, Complete, and Verifiable example 来重现该问题。
我只是通过将 header 的文本添加到 Run_Click
子中来找到解决方案。每次你 运行 事件时,这又会将文本附加到现有文件中并将其放在第一行(header 行)。有用!
下面是我使用的代码:
Private Sub Run_Click(sender As Object, e As EventArgs) Handles Run.Click
Dim Path As String = "C:\bin\Debug\TimerTest.csv"
My.Computer.FileSystem.WriteAllText(Path, "Date and Time, Tag Name, Value", False) 'add
Timer1.Enabled = True
End Sub
如何使用 VB.NET 代码仅在第一行生成 .csv header?下面是我试过的代码,但它写在 .csv 文件的顶部和列上。
Try
My.Computer.FileSystem.WriteAllText("C:\bin\Debug\TimerTest.csv",
"Date and Time,Tag Name, Value" & vbCrLf &
timeUtc & "," & Tag_name_Read & "," & itemValues(0).Value.ToString, True)
'Date and Time, Tag Name, Value are my header. they are suppose to be on the first line
Catch ex As Exception
End Try
If daServerMgt.IsConnected Then
daServerMgt.Disconnect()
End If
Update Link Showing the csv file)
我只想在顶部显示 header(日期和时间 | 标签名称 | 值),我不希望它出现在列中。它附加它是因为 My.Computer.FileSystem.WriteAllText(File, Text, true)
我该如何解决这个问题?
除了 @Enigmativity 提到的问题之外,您的代码似乎没有任何问题。
我的旧机器最近崩溃了,我还没有在我的 b运行d 新机器上安装 Visual Studio,但我在 C:\Temp
文件夹中创建了一个文件名为 TimerTest.vb
,内容如下:
Imports System
Module Module1
Public Structure Struct1
Public Value As Integer
Sub New(ByVal _Value As Integer)
Value = _Value
End Sub
End Structure
Sub Main()
Dim timeUtc As DateTime = DateTime.Now()
Dim Tag_name_Read As String = "FortyTwo"
Dim itemValues = New Struct1() { New Struct1(42), New Struct1(43), New Struct1(44)}
Try
'My.Computer.FileSystem.WriteAllText("C:\bin\Debug\TimerTest.csv",
My.Computer.FileSystem.WriteAllText("C:\Temp\TimerTest.csv",
"Date and Time,Tag Name, Value" & vbCrLf &
timeUtc & "," & Tag_name_Read & "," & itemValues(0).Value.ToString, True)
'Date and Time, Tag Name, Value are my header. they are suppose to be on the first line
Catch ex As Exception
End Try
'If daServerMgt.IsConnected Then
' daServerMgt.Disconnect()
'End If
End Sub
End Module
然后我通过 运行 在命令 Window 中编译了以下语句(vbc.exe
的父文件夹已经在我的 %PATH%
中):
vbc C:\Temp\TimerTest.vb
在我运行生成TimerTest.exe
文件后,在C:\Temp
中生成了一个TimerTest.csv
文件,内容如下:
Date and Time,Tag Name, Value<br>
10/27/2016 9:07:31 PM,FortyTwo,42
当我双击 TimerTest.csv
文件时,它在 Excel 中打开没有任何问题:
因此,问题一定出在代码的其他地方。为了让我们更容易地帮助您,请提供一个 Minimal, Complete, and Verifiable example 来重现该问题。
我只是通过将 header 的文本添加到 Run_Click
子中来找到解决方案。每次你 运行 事件时,这又会将文本附加到现有文件中并将其放在第一行(header 行)。有用!
下面是我使用的代码:
Private Sub Run_Click(sender As Object, e As EventArgs) Handles Run.Click
Dim Path As String = "C:\bin\Debug\TimerTest.csv"
My.Computer.FileSystem.WriteAllText(Path, "Date and Time, Tag Name, Value", False) 'add
Timer1.Enabled = True
End Sub