vb listview怎样只读取txt文件的前10行?
我现在的代码是这样的,读取的是全部txt内容,怎样修改,使它只读取前十行?请一定在下面这个代码基础上修改,不要另外新写代码。DimstrAsString,TMP,LstA...
我现在的代码是这样的,读取的是全部txt内容,怎样修改,使它只读取前十行?请一定在下面这个代码基础上修改,不要另外新写代码。
Dim str As String, TMP, Lst As ListItem
Open App.Path & "\aaa.txt" For Input As #1
Do Until EOF(1)
Line Input #1, str
TMP = Split(str, " ,")
Set Lst = ListView1.ListItems.Add(, , TMP(0))
Lst.ListSubItems.Add , , TMP(1)
Lst.ListSubItems.Add , , TMP(2)
Lst.ListSubItems.Add , , TMP(3)
Loop
Close #1
补充:
txt文件内容是这样的:
123,456,789
abc,def,999
…… 展开
Dim str As String, TMP, Lst As ListItem
Open App.Path & "\aaa.txt" For Input As #1
Do Until EOF(1)
Line Input #1, str
TMP = Split(str, " ,")
Set Lst = ListView1.ListItems.Add(, , TMP(0))
Lst.ListSubItems.Add , , TMP(1)
Lst.ListSubItems.Add , , TMP(2)
Lst.ListSubItems.Add , , TMP(3)
Loop
Close #1
补充:
txt文件内容是这样的:
123,456,789
abc,def,999
…… 展开
3个回答
展开全部
Option Explicit
Private Sub Command1_Click()
Dim fn As Integer, i As Integer, Pfile As String, Fstr
Dim itmX As ListItem
fn = FreeFile
Pfile = "D:\ISO\readme.txt"
'开启文件,每行以数组变量保存
Open Pfile For Binary As #fn
Fstr = Split(Input(LOF(fn), fn), vbCrLf, -1)
Close #fn
'把数据放入ListView1
For i = LBound(Fstr) To UBound(Fstr)
Set itmX = ListView1.ListItems.Add(, , cstr(split(Fstr(i)," ",-1)(0))) '默认占用aa
itmX.SubItems(ListView1.ColumnHeaders("bb").SubItemIndex) = CStr(split(Fstr(i)," ",-1)(1))
itmX.SubItems(ListView1.ColumnHeaders("cc").SubItemIndex) = CStr(split(Fstr(i)," ",-1)(2))
If i >= 9 Then Exit For
Next i
ListView1.Refresh
End Sub
Private Sub Form_Load()
'设计标题
ListView1.ColumnHeaders.Add , "aa", "第一列", ListView1.Width / 3
ListView1.ColumnHeaders.Add , "bb", "第二列", ListView1.Width / 3, lvwColumnLeft
ListView1.ColumnHeaders.Add , "cc", "第三列", ListView1.Width / 3, lvwColumnCenter
ListView1.View = lvwReport
End Sub
'依照最后提示每行以空格处理,重新修改代码。
Private Sub Command1_Click()
Dim fn As Integer, i As Integer, Pfile As String, Fstr
Dim itmX As ListItem
fn = FreeFile
Pfile = "D:\ISO\readme.txt"
'开启文件,每行以数组变量保存
Open Pfile For Binary As #fn
Fstr = Split(Input(LOF(fn), fn), vbCrLf, -1)
Close #fn
'把数据放入ListView1
For i = LBound(Fstr) To UBound(Fstr)
Set itmX = ListView1.ListItems.Add(, , cstr(split(Fstr(i)," ",-1)(0))) '默认占用aa
itmX.SubItems(ListView1.ColumnHeaders("bb").SubItemIndex) = CStr(split(Fstr(i)," ",-1)(1))
itmX.SubItems(ListView1.ColumnHeaders("cc").SubItemIndex) = CStr(split(Fstr(i)," ",-1)(2))
If i >= 9 Then Exit For
Next i
ListView1.Refresh
End Sub
Private Sub Form_Load()
'设计标题
ListView1.ColumnHeaders.Add , "aa", "第一列", ListView1.Width / 3
ListView1.ColumnHeaders.Add , "bb", "第二列", ListView1.Width / 3, lvwColumnLeft
ListView1.ColumnHeaders.Add , "cc", "第三列", ListView1.Width / 3, lvwColumnCenter
ListView1.View = lvwReport
End Sub
'依照最后提示每行以空格处理,重新修改代码。
追问
如果源文件是excel格式文件怎样导入呢?.xls
追答
因为vb是对象物件导向的编辑程序,所以你要先从VB的引用Microsoft Excel 11.0 Object Library
然后从VB代码启动 .xls文件获取数据,再把数据填入VB ListView 控件中。 这整个过程你最好先学习一下,有问题再继续提出。
度娘查找 【VB Microsoft Excel 11.0 Object Library】 字串,会有你需要的许多参考内容。
展开全部
很简单呀
Dim str As String, TMP, Lst As ListItem
Open App.Path & "\aaa.txt" For Input As #1
for i=0 to 9
Line Input #1, str
TMP = Split(str, " ,")
Set Lst = ListView1.ListItems.Add(, , TMP(0))
Lst.ListSubItems.Add , , TMP(1)
Lst.ListSubItems.Add , , TMP(2)
Lst.ListSubItems.Add , , TMP(3)
next
Close #1
追问
提示下界越标
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-03-20
展开全部
for i=1 to 10……
追问
请看下补充提问。里面代码怎样修改?
追答
补充代码在哪里?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询