如何使用VB6编写CSV文件

如何使用VB6编写CSV文件,csv,vb6,filesystemobject,Csv,Vb6,Filesystemobject,如何使用VB6在.csv文件中写入每列?我使用文件系统对象.writeline在.csv文件中读取和写入,但我的问题是它只在一列中写入。谁能帮帮我吗 Dim fso As New FileSystemObject Dim fsoStream As TextStream Set fsoStream = fso.CreateTextFile("C:\Users\Users\Desktop\File\Sample.csv", True) fsoStream.WriteLine "Like This

如何使用
VB6
.csv
文件中写入每列?我使用
文件系统对象
.writeline
在.csv文件中读取和写入,但我的问题是它只在一列中写入。谁能帮帮我吗

Dim fso As New FileSystemObject
Dim fsoStream As TextStream

Set fsoStream = fso.CreateTextFile("C:\Users\Users\Desktop\File\Sample.csv", True)

fsoStream.WriteLine "Like This is what I have tried" 'this must be written in the first column
fsoStream.WriteLine "But this is a sample only" 'this must be written in the first column
fsoStream.WriteLine "Sample 1" 'this must be written in the second column
fsoStream.WriteLine "Sample 1" 'this must be written in the second column
fsoStream.WriteLine "Sample 2" 'this must be written in the third column
fsoStream.WriteLine "Sample 2" 'this must be written in the third column

fsoStream.Close
Set fsoStream = Nothing
Set fso = Nothing
此图片必须是输出。

但这就是我在使用上述代码时得到的

假设您事先知道要写入哪些列,可以使用以下内容(可选标题行):

Dim iFileNo As Integer

iFileNo = FreeFile

Open sFileName For Output As #iFileNo
Write #iFileNo, "ID", "Name", "Age", "Address"
Write #iFileNo, 1, "Person #1", 42, "Place #1"
Write #iFileNo, 2, "Person #2", 57, "Place #2"
Close #iFileNo
你可以这样读:

Dim sHeaders As String
Dim iFileNo As Integer
Dim lID As Long
Dim sName As String
Dim iAge As Integer
Dim sAddress As String

iFileNo = FreeFile

Open sFileName For Input As #iFileNo
Line Input #iFileNo, sHeaders
Do Until Eof(iFileNo)
    Input #iFileNo, lID, sName, iAge, sAddress
Loop
Close #iFileNo

假设您事先知道要写入的列,可以使用以下内容(可选标题行):

Dim iFileNo As Integer

iFileNo = FreeFile

Open sFileName For Output As #iFileNo
Write #iFileNo, "ID", "Name", "Age", "Address"
Write #iFileNo, 1, "Person #1", 42, "Place #1"
Write #iFileNo, 2, "Person #2", 57, "Place #2"
Close #iFileNo
你可以这样读:

Dim sHeaders As String
Dim iFileNo As Integer
Dim lID As Long
Dim sName As String
Dim iAge As Integer
Dim sAddress As String

iFileNo = FreeFile

Open sFileName For Input As #iFileNo
Line Input #iFileNo, sHeaders
Do Until Eof(iFileNo)
    Input #iFileNo, lID, sName, iAge, sAddress
Loop
Close #iFileNo

您可以为每一行构建一个字符串,当行字符串完成后,将其写入文件

例如,使用您文章中的代码:

Dim strLine As String
Dim fso As New FileSystemObject
Dim fsoStream As TextStream

Set fsoStream = fso.CreateTextFile("C:\Users\Users\Desktop\File\Sample.csv", True)

'prepare the first row
strLine = "Like This is what I have tried" 'this must be written in the first column
'write the first row
fsoStream.WriteLine strLine

'prepare the second row
strLine = "But this is a sample only"      'this must be written in the first column
strLine = strLine & "," & "Sample 1"       'this must be written in the second column
'write the seconde row
fsoStream.WriteLine strLine

'prepare the third row
strLine = ""                               'an empty first column
strLine = strLine & "," & "Sample 1"       'this must be written in the second column
strLine = strLine & "," & "Sample 2"       'this must be written in the third column
'write the third row
fsoStream.WriteLine strLine

'prepare the fourth row
strLine = ""                               'an empty first column
strLine = strLine & ","                    'an empty second column
strLine = strLine & "," & "Sample 2"       'this must be written in the third column
'write the fourth row
fsoStream.WriteLine strLine

fsoStream.Close
Set fsoStream = Nothing
Set fso = Nothing
这不会给出您在所需输出图片中发布的结果,但这也是因为您在原始代码中有额外的写入操作

只需删除代码中多余的行,结果将与您在图片中发布的一样。
我相信您可以找到我所说的额外行:)

您可以为每一行构建一个字符串,当行字符串完成后,将其写入文件

例如,使用您文章中的代码:

Dim strLine As String
Dim fso As New FileSystemObject
Dim fsoStream As TextStream

Set fsoStream = fso.CreateTextFile("C:\Users\Users\Desktop\File\Sample.csv", True)

'prepare the first row
strLine = "Like This is what I have tried" 'this must be written in the first column
'write the first row
fsoStream.WriteLine strLine

'prepare the second row
strLine = "But this is a sample only"      'this must be written in the first column
strLine = strLine & "," & "Sample 1"       'this must be written in the second column
'write the seconde row
fsoStream.WriteLine strLine

'prepare the third row
strLine = ""                               'an empty first column
strLine = strLine & "," & "Sample 1"       'this must be written in the second column
strLine = strLine & "," & "Sample 2"       'this must be written in the third column
'write the third row
fsoStream.WriteLine strLine

'prepare the fourth row
strLine = ""                               'an empty first column
strLine = strLine & ","                    'an empty second column
strLine = strLine & "," & "Sample 2"       'this must be written in the third column
'write the fourth row
fsoStream.WriteLine strLine

fsoStream.Close
Set fsoStream = Nothing
Set fso = Nothing
这不会给出您在所需输出图片中发布的结果,但这也是因为您在原始代码中有额外的写入操作

只需删除代码中多余的行,结果将与您在图片中发布的一样。
我相信你可以找到我所说的额外的行:)

尽管我看不出有理由使用FileSystemObject,但是使用VB中的标准打开、关闭和打印功能要容易得多

要创建所需的输出,可以使用以下代码:

Dim intFile As Integer
Dim strFile As String
Dim strLine As String

'open the file
intFile = FreeFile
strFile = "c:\temp\test.csv"
Open strFile For Output As #intFile

  'prepare the first row
  strLine = "Like This is what I have tried" 'this must be written in the first column
  strLine = strLine & "," & "Sample 1"       'this must be written in the second column
  strLine = strLine & "," & "Sample 2"       'this must be written in the third column
  'write the first row
  Print #intFile, strLine

  'prepare the second row
  strLine = "But this is a sample only"      'this must be written in the first column
  strLine = strLine & "," & "Sample 1"       'this must be written in the second column
  strLine = strLine & "," & "Sample 2"       'this must be written in the third column
  'write the seconde row
  Print #intFile, strLine

'close the file
Close #intFile
您也可以通过将vbCrLF添加到前一行,然后将下一行添加到字符串中,从而在1个字符串中构建整个csv,而不是单独写入每一行

strTotalLines = strTotalLines & vbCrLf & strNewLine

并在1个print语句的末尾将所有行写入文件

我认为没有理由使用FileSystemObject,使用VB中的标准打开、关闭和打印功能要容易得多

要创建所需的输出,可以使用以下代码:

Dim intFile As Integer
Dim strFile As String
Dim strLine As String

'open the file
intFile = FreeFile
strFile = "c:\temp\test.csv"
Open strFile For Output As #intFile

  'prepare the first row
  strLine = "Like This is what I have tried" 'this must be written in the first column
  strLine = strLine & "," & "Sample 1"       'this must be written in the second column
  strLine = strLine & "," & "Sample 2"       'this must be written in the third column
  'write the first row
  Print #intFile, strLine

  'prepare the second row
  strLine = "But this is a sample only"      'this must be written in the first column
  strLine = strLine & "," & "Sample 1"       'this must be written in the second column
  strLine = strLine & "," & "Sample 2"       'this must be written in the third column
  'write the seconde row
  Print #intFile, strLine

'close the file
Close #intFile
您也可以通过将vbCrLF添加到前一行,然后将下一行添加到字符串中,从而在1个字符串中构建整个csv,而不是单独写入每一行

strTotalLines = strTotalLines & vbCrLf & strNewLine

并在1个打印语句的末尾将所有行写入文件

,正如其他人指出的那样,您一次写入一整行文本。文本文件没有列的概念,因此您需要重新考虑如何准备数据。用户tony bd建议您使用断开连接的记录集,我认为他是对的。记录集允许您轻松地将数据分配给列。将所有数据处理到记录集中后,可以将其保存到csv文件中

添加对Microsoft ActiveX数据对象2.8库的引用

Option Explicit

Private mMyRs As ADODB.Recordset

Private Sub Form_Load()

    CreateRecordset

End Sub

Private Sub Command1_Click()

    SaveRecordset

End Sub

Private Sub CreateRecordset()

    Set mMyRs = New ADODB.Recordset
    With mMyRs
        Set .ActiveConnection = Nothing
        .CursorLocation = adUseClient
    End With

    With mMyRs.Fields
        .Append "Column1", adVarChar, 40
        .Append "Column2", adVarChar, 20
        .Append "Column3", adVarChar, 20
    End With
    mMyRs.Open

    mMyRs.AddNew
    mMyRs.Fields("Column1").Value = "Like This is what I have tried"
    mMyRs.AddNew
    mMyRs.Fields("Column1").Value = "But this is a sample only"
    mMyRs.MoveFirst
    mMyRs.Fields("Column2").Value = "Sample 1"
    mMyRs.MoveNext
    mMyRs.Fields("Column2").Value = "Sample 1"
    mMyRs.MoveFirst
    mMyRs.Fields("Column3").Value = "Sample 2"
    mMyRs.MoveNext
    mMyRs.Fields("Column3").Value = "Sample 2"

End Sub

Private Sub SaveRecordset()
    Dim fHndl As Integer
    Dim strCsvFile As String

    strCsvFile = "C:\Temp\MyCSV.csv"
    fHndl = FreeFile

    Open strCsvFile For Output As fHndl
    mMyRs.MoveFirst
    While mMyRs.EOF = False
        Print #fHndl, mMyRs.Fields("Column1").Value & "," & mMyRs.Fields("Column2").Value & "," & mMyRs.Fields("Column3").Value
        mMyRs.MoveNext
    Wend
    Close #fHndl

End Sub
结果


正如其他人所指出的,你一次写下一整行文字。文本文件没有列的概念,因此您需要重新考虑如何准备数据。用户tony bd建议您使用断开连接的记录集,我认为他是对的。记录集允许您轻松地将数据分配给列。将所有数据处理到记录集中后,可以将其保存到csv文件中

添加对Microsoft ActiveX数据对象2.8库的引用

Option Explicit

Private mMyRs As ADODB.Recordset

Private Sub Form_Load()

    CreateRecordset

End Sub

Private Sub Command1_Click()

    SaveRecordset

End Sub

Private Sub CreateRecordset()

    Set mMyRs = New ADODB.Recordset
    With mMyRs
        Set .ActiveConnection = Nothing
        .CursorLocation = adUseClient
    End With

    With mMyRs.Fields
        .Append "Column1", adVarChar, 40
        .Append "Column2", adVarChar, 20
        .Append "Column3", adVarChar, 20
    End With
    mMyRs.Open

    mMyRs.AddNew
    mMyRs.Fields("Column1").Value = "Like This is what I have tried"
    mMyRs.AddNew
    mMyRs.Fields("Column1").Value = "But this is a sample only"
    mMyRs.MoveFirst
    mMyRs.Fields("Column2").Value = "Sample 1"
    mMyRs.MoveNext
    mMyRs.Fields("Column2").Value = "Sample 1"
    mMyRs.MoveFirst
    mMyRs.Fields("Column3").Value = "Sample 2"
    mMyRs.MoveNext
    mMyRs.Fields("Column3").Value = "Sample 2"

End Sub

Private Sub SaveRecordset()
    Dim fHndl As Integer
    Dim strCsvFile As String

    strCsvFile = "C:\Temp\MyCSV.csv"
    fHndl = FreeFile

    Open strCsvFile For Output As fHndl
    mMyRs.MoveFirst
    While mMyRs.EOF = False
        Print #fHndl, mMyRs.Fields("Column1").Value & "," & mMyRs.Fields("Column2").Value & "," & mMyRs.Fields("Column3").Value
        mMyRs.MoveNext
    Wend
    Close #fHndl

End Sub
结果


其他答案很好,但对代码最简单的修改如下:

fsoStream.WriteLine "Like This is what I have tried, Sample1, Sample2" 
fsoStream.WriteLine "But this is a sample only, Sample1, Sample2" 
取而代之的是:

fsoStream.WriteLine "Like This is what I have tried" 'this must be written in the first column
fsoStream.WriteLine "But this is a sample only" 'this must be written in the first column
fsoStream.WriteLine "Sample 1" 'this must be written in the second column
fsoStream.WriteLine "Sample 1" 'this must be written in the second column
fsoStream.WriteLine "Sample 2" 'this must be written in the third column
fsoStream.WriteLine "Sample 2" 'this must be written in the third column

关键是“csv”表示“逗号分隔值”。这意味着每一列必须用逗号分隔,并且在同一行上。新行意味着新行,正如您自己已经看到的。

其他答案很好,但对代码最简单的修改如下:

fsoStream.WriteLine "Like This is what I have tried, Sample1, Sample2" 
fsoStream.WriteLine "But this is a sample only, Sample1, Sample2" 
取而代之的是:

fsoStream.WriteLine "Like This is what I have tried" 'this must be written in the first column
fsoStream.WriteLine "But this is a sample only" 'this must be written in the first column
fsoStream.WriteLine "Sample 1" 'this must be written in the second column
fsoStream.WriteLine "Sample 1" 'this must be written in the second column
fsoStream.WriteLine "Sample 2" 'this must be written in the third column
fsoStream.WriteLine "Sample 2" 'this must be written in the third column

关键是“csv”表示“逗号分隔值”。这意味着每一列必须用逗号分隔,并且在同一行上。新行表示新行,正如您自己已经看到的。

请提供您尝试的代码,您必须添加自己的逗号。fso.writeline var1&“,”和var2@tonybd我编辑我的问题并添加一些图片。我怎么能这样做呢?使用:fsoStream.WriteLine data1+您的分隔符+data2+您的分隔符+data3请为您尝试的内容提供代码您必须添加自己的逗号。fso.writeline var1&“,”和var2@tonybd我编辑我的问题并添加一些图片。我怎样才能做到这一点呢?使用:fsoStream.WriteLine data1+您的_分隔符+data2+您的_分隔符+data3数据被动态写入
csv文件
假设解析数据的第一行被完成,然后它被写入csv文件的第一列,然后解析数据的第二行被完成,然后它将在csv文件的第二列等等..@bebebe抱歉-我不明白你的意思。最后一句评论中没有标点符号。为了便于解释。。它将在CSV文件中水平写入,而不是垂直写入。@bebe该方法称为“WriteLine”。因此,它完全按照它所说的去做。我建议您使用内置的VB Write语句,就像在我的代码中一样。我要做的是创建一个断开连接的记录集。这是最终的数据类型。然后按照您的意愿编写数据。断开连接的记录集可以将自己保存为XML或二进制格式的文件。或者您可以通读它,一次一行写入每一列,比如fso.writeline rs.Fields(“column1”).Value&“,“rs.Fields(“column2”).Value。也可以使用数组。请参阅我的帖子此处感兴趣的示例代码位于底部。假设解析数据的第一行