如何添加一列?

时间:2009-09-13 11:00:07

标签: vb6

使用VB6

Dim fso As FileSystemObject
Dim TS As TextStream
Dim TempS As String
Dim Final As String
Set fso = New FileSystemObject
Set TS = fso.OpenTextFile(1.txt, ForReading)
Final = TS.ReadAll
Do Until TS.AtEndOfStream
    TempS = TS.ReadLine
    Final = Final & TempS & vbCrLf
Loop
TS.Close
Set TS = fso.OpenTextFile(App.Path & "\Staff.txt", ForAppending, True)
    TS.Write Final
TS.Close
Set TS = Nothing
Set fso = Nothing

上面的代码工作正常,但我想在Staff.txt中写一行时再添加一行。

TEXTFILE

的1.txt

M3,4331,57,0,3,,20090405,153601,8193,3,0,,,,
M3,4440,59,0,3,,20090405,172110,8193,3,0,,,,
M3,4439,66,0,1,,20090405,172106,8193,3,0,,,,
M3,4374,68,0,1,,20090405,165003,8193,3,0,,,,

预期产出

将文件写为Staff.txt时

Col1, col2, col3, col4, col5, col6..
M3,4331,57,0,3,,20090405,153601,8193,3,0,,,,
M3,4440,59,0,3,,20090405,172110,8193,3,0,,,,
M3,4439,66,0,1,,20090405,172106,8193,3,0,,,,
M3,4374,68,0,1,,20090405,165003,8193,3,0,,,,

我想在上面添加一行col1, col2, col3…. So on…,.如何修改代码?

需要VB6代码帮助

2 个答案:

答案 0 :(得分:1)

首先,你应该有一个字符串变量来将列名写入:

Dim header as String
Dim i as Integer
For i=0 To NumberOfColumns-1
  header=header &"Col" & i & ","
Next i

然后你应该在写入任何其他文本之前将标题写入TextStream :(在TS.Write Final之前)

TS.Write Header & vbCrLf
TS.Write Final

答案 1 :(得分:0)

Set TS = fso.OpenTextFile(App.Path & "\Staff.txt", ForAppending, True)
    TS.WriteLine("col1, col2, col3, col4, col5, col6")
    TS.Write Final
TS.Close

另外,我觉得这段代码正在运行。例如,您有fso.OpenTextFile(1.txt, ForReading),应为"1.txt"