如何使用VB6写入CSV文件

时间:2014-04-23 06:47:08

标签: csv vb6 filesystemobject

如何使用.csvVB6文件中编写每列?我在.csv文件中使用File System Object .writeline 读取和写入但我的问题是它只能写入仅一列。有人可以帮助我。

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

此图片必须是输出。

enter image description here

但这是我使用上面的代码所得到的 enter image description here

5 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

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

例如使用帖子中的代码:

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

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

只需删除代码中的额外行,结果将与您在图片中发布的结果一致。 我相信你能找到我正在谈论的额外线路:)

答案 2 :(得分:1)

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

添加对Microsoft ActiveX Data Objects 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

结果

csv results image

答案 3 :(得分:1)

其他答案都很好,但对代码的最简单修改就是:

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”表示“以逗号分隔的值”。这意味着每列必须用逗号分隔,并在同一行上。新行意味着新行,正如您自己已经看到的那样。

答案 4 :(得分:0)

我认为没有理由使用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然后将下一行添加到字符串

来构建整个csv中的1个字符串。
strTotalLines = strTotalLines & vbCrLf & strNewLine

并在1个打印语句中将总行写入文件末尾