请高手指教:在VB中怎样把变量如10——99个数分4列写入文件中。而print #1只能排一列。

i是一个变量,其中含很多数据项。
Open "e:\test.txt" For Input As #1 ‘打开一个文件
print #1,i '写入文件,这时文件中只显示一竖列,而需要多列显示。
close #1
打开 e:\test.txt 时文件中要多列显示数据项。

新建一个文本文档,将以下代码复制到文本文档里面,并改名为form1.frm,用vb6.0打开form1.frm,运行之。。。
我按照你的要求给改了一下,你在第一个文本框里输入 最小值(比如你说的10)。在第二个文本框里输入最大值(比如你说的99),点一下写入,然后你到e:\test.txt 看看是不是你要的结果出来了。
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 4260
ClientLeft = 60
ClientTop = 450
ClientWidth = 6600
LinkTopic = "Form1"
ScaleHeight = 4260
ScaleWidth = 6600
StartUpPosition = 3 '窗口缺省
WindowState = 1 'Minimized
Begin VB.CommandButton Command1
Caption = "command1"
Height = 495
Left = 960
TabIndex = 2
Top = 1800
Width = 1695
End
Begin VB.TextBox Text2
Height = 735
Left = 3720
MultiLine = -1 'True
ScrollBars = 2 'Vertical
TabIndex = 1
Text = "Form1.frx":0000
Top = 600
Width = 1935
End
Begin VB.TextBox Text1
Height = 735
Left = 600
MultiLine = -1 'True
ScrollBars = 2 'Vertical
TabIndex = 0
Text = "Form1.frx":0006
Top = 600
Width = 2055
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private Sub Command1_Click()
Dim aa As String, i As Long
For i = Val(Text1.Text) To Val(Text2.Text)
If i Mod 4 = 0 Then
aa = aa & Chr(13) & Chr(10) & Format(i, "00") & " "
Else
aa = aa & Format(i, "00") & " "
End If
Next i
Open "e:\test.txt " For Output As #1
Seek #1, 1
Print #1, aa
Close #1
End Sub

Private Sub Form_Load()
Text1.Text = "请在这里输入要写入数字的最小值"
Text2.Text = "请在这里输入要写入数字的最大值"
Command1.Caption = "写入"

End Sub
温馨提示:内容为网友见解,仅供参考
第1个回答  2008-12-04
给你个最简单的吧,随机生成90个数,在文本中以四列形式显示。
Private Sub Command2_Click()
Dim i, j, a(1 To 25, 1 To 4) As Integer
For i = 1 To 25
For j = 1 To 4
a(i, j) = Int(Rnd() * 90 + 10)
Next j, i
Open "d:\data.txt" For Output As #1
For i = 1 To 25
Print #1, Tab(1); a(i, 1); Tab(6); a(i, 2); Tab(11); a(i, 3); Tab(16); a(i, 4)
Next
Close #1
End Sub
完全能够满足你的要求了。
第2个回答  2008-12-04
你是想"e:\test.txt"这个文件的内容格式跟一个数据库的格式一样的吧,..
如果你非用open文本的代码来操作 我看只能对i这个变量的内容进行操作来实现你的要求了..
既然这么麻烦 为什么不用操作数据库的SQL语言呢..

增加一列:alter table tabelname add columnname varchar2(8) NULL
删除一列:alter table tablename drop column columnname
第3个回答  2008-12-04
用for循环
让变量 i 从1循环到4,一次写入一列
第4个回答  2008-12-04
也许下面的正是你需要的:
Private Sub Command1_Click()
Dim i As Integer, j As Integer, k As Integer, s As String
Open "c:\1.txt" For Output As #1
For i = 1 To 23
s = ""
For j = 1 To 4
k = 9 + i + (j - 1) * 23
If k < 100 Then s = s & k & vbTab
Next
Print #1, s
Next
Close #1
msgbox "文件写入完毕!"
End Sub本回答被网友采纳
相似回答
大家正在搜