vb txt excel

来源:百度知道 编辑:UC知道 时间:2024/05/15 10:34:54
逐行读取txt文本,然后查找我要找的字符串,如果有,读取该行该字符串后的内容,并写入excel,如果没有接着读取下一行,注意:文本中有空行
先给50,写成后在加100,谢谢

顺序读取文本每行的代码示例
Dim i As Integer
Dim str() As String
Dim flg As Boolean
Dim tmpString As String

’读取文本中的每一行
Open App.Path + "\A.txt" For Input As #1
Do While Not EOF(1)
i = i + 1
ReDim Preserve str(i)
Line Input #1, str(i)
Loop
Close #1

' 使用InStr函数判断是否有需要查找的字符串
For i = 0 To UBound(str)
If InStr(1, str(i), txtStr.Text) > 0 Then
flg = True
End If

If flg And i < UBound(str) Then
tmpString = tmpString + str(i + 1)
End If
Next i

VB写入Excel的方法
Private Sub Command1_Click()
Dim xlExcel As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim AppExcel As Object
Dim str As String
str = App.Path & "\11.xls"

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False '设置excel对象