写入: dim i as long Open "f:\abc.txt" For Output As #1 Print #1,"时间" & vbtab & "名称1" & vbtab & "名称2" & vbtab & "名称3" & vbtab & "名称4" & vbCrLf for i=1 to 2 Print #1,str(i) & vbtab & "项目1" & vbtab & "项目2" & vbtab & "项目3" & vbtab & "项目4" & vbCrLf next i Close #1读出: Dim s As String, ls_Content() As String,ls_ContentA() As String dim i as long,ii as long Dim LogCount As Long Open "f:\abc.txt" For Input As #1 s = StrConv(InputB(LOF(1), #1), vbUnicode) Close #1 ls_Content = Split(s, vbCrLf) LogCount =UBound(ls_Content, 1)
'读出来的全部代码如下 读出: Dim s As String, ls_Content() As String,ls_ContentA() As String dim i as long,ii as long Dim LogCount As Long,LogCountA As Long Open "f:\abc.txt" For Input As #1 s = StrConv(InputB(LOF(1), #1), vbUnicode) Close #1 ls_Content = Split(s, vbCrLf) LogCount =UBound(ls_Content, 1) for i=0 to LogCount debug.print ls_Content(i)'打印出每行的信息 ls_ContentA = Split(ls_Content(i), vbtab) LogCountA =UBound(ls_ContentA, 1) for ii=0 to LogCountA debug.print ls_ContentA(ii)'打印每行中的每个项目信息 next ii next i
当列数变化的时候 该怎么办呢 for i=1 to 2 Print #1,str(i) & vbtab & "项目1" & vbtab & "项目2" & vbtab & "项目3" & vbtab & "项目4" & vbCrLf next i此处项目数是变量 怎么写在同一行呢?
dim a as string,b as string a="项目x" b="123456" for i=1 to 2 Print #1,str(i) & vbtab & a & vbtab & a & vbtab & b & vbtab & "项目4" & vbCrLf next i
用CSV文件吧!变量之间用","分割,还可以用Excel编辑。 写入的时候可以用 Set fs = CreateObject("Scripting.FileSystemObject") Set a = fs.CreateTextFile("c:\testfile.txt", True) a.WriteLine("1,2,3") a.Close 读入的时候 Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault) s = ts.ReadLine
dim i as long
Open "f:\abc.txt" For Output As #1
Print #1,"时间" & vbtab & "名称1" & vbtab & "名称2" & vbtab & "名称3" & vbtab & "名称4" & vbCrLf
for i=1 to 2
Print #1,str(i) & vbtab & "项目1" & vbtab & "项目2" & vbtab & "项目3" & vbtab & "项目4" & vbCrLf
next i
Close #1读出:
Dim s As String, ls_Content() As String,ls_ContentA() As String
dim i as long,ii as long
Dim LogCount As Long
Open "f:\abc.txt" For Input As #1
s = StrConv(InputB(LOF(1), #1), vbUnicode)
Close #1
ls_Content = Split(s, vbCrLf)
LogCount =UBound(ls_Content, 1)
读出:
Dim s As String, ls_Content() As String,ls_ContentA() As String
dim i as long,ii as long
Dim LogCount As Long,LogCountA As Long
Open "f:\abc.txt" For Input As #1
s = StrConv(InputB(LOF(1), #1), vbUnicode)
Close #1
ls_Content = Split(s, vbCrLf)
LogCount =UBound(ls_Content, 1)
for i=0 to LogCount
debug.print ls_Content(i)'打印出每行的信息
ls_ContentA = Split(ls_Content(i), vbtab)
LogCountA =UBound(ls_ContentA, 1)
for ii=0 to LogCountA
debug.print ls_ContentA(ii)'打印每行中的每个项目信息
next ii
next i
for i=1 to 2
Print #1,str(i) & vbtab & "项目1" & vbtab & "项目2" & vbtab & "项目3" & vbtab & "项目4" & vbCrLf
next i此处项目数是变量 怎么写在同一行呢?
a="项目x"
b="123456"
for i=1 to 2
Print #1,str(i) & vbtab & a & vbtab & a & vbtab & b & vbtab & "项目4" & vbCrLf
next i
split 函数比较好用,首先以vbcrlf为分隔符把每一行读进数组中
然后以" "或vbtab为分隔符把数组中每个元素再放到一个数组中(结果为2维数组)
写入的时候可以用
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("c:\testfile.txt", True)
a.WriteLine("1,2,3")
a.Close
读入的时候
Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault)
s = ts.ReadLine