| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 782 人关注过本帖
标题:请教:vb如何快速处理较大的txt文件?
取消只看楼主 加入收藏
qbh555
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2012-12-26
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:6 
请教:vb如何快速处理较大的txt文件?
附件.rar (184.9 KB)
用vb快速处理txt,提取需要的信息输出保存在excel中如附件,请问代码怎么写呢?之前我用 Open Name For Input As #1 ,Do While Not EOF(1) ,Line Input #1, txtline 来处理,但处理速度非常的慢,半个小时都出不来结果。求教了,谢谢各位。

[ 本帖最后由 qbh555 于 2013-5-20 20:44 编辑 ]
搜索更多相关主题的帖子: 如何 
2013-05-20 20:27
qbh555
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2012-12-26
收藏
得分:0 
回复 3楼 风吹过b
是啊,文件很大,(其实是有好几个这样的文件),边读边输入到EXCEL很慢,全部读取完再一下写入也很慢啊。
2013-05-21 08:59
qbh555
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2012-12-26
收藏
得分:0 
回复 2楼 bczgvip
悲催啊
2013-05-21 09:04
qbh555
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2012-12-26
收藏
得分:0 
回复 6楼 风吹过b
CSV格式输出也可以只要得到结果,然后处理速度只要不用登上半个小时没反应就可以,请教代码如何编写呢?
2013-05-22 14:31
qbh555
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2012-12-26
收藏
得分:0 
回复 3楼 风吹过b
以下可以处理,就是速度非常的慢,请教快速处理方法。


Private Sub Command1_Click()
'/////////////////////////////////////////////////////////////////////////
  MsgBox "选择输出路径"
   With Application.FileDialog(msoFileDialogFolderPicker)
    If (.Show = -1) Then
        mypathout = .SelectedItems(1)
        End If
    End With
    If mypathout = "" Then
        Exit Sub
    End If
   mypathout = mypathout + "\"
'///////////////////////////////////////////////////////////////////////////
  Dim sig1(65535) As Variant
  Dim sig2(65535) As Variant
  Dim sig3(65535) As Variant
  Dim sig4(65535) As Variant
  Dim sig5(65535) As Variant
  Dim sig6(65535) As Variant
  Dim sig7(65535) As Variant
  Dim sig8(65535) As Variant
  Dim sig9(65535) As Variant
  Dim sig10(65535) As Variant
  Dim sig11(65535) As Variant
  Dim sig12(65535) As Variant
  g1 = 1
  g2 = 1
  g3 = 1
  g4 = 1
  g5 = 1
  g6 = 1
  g7 = 1
  g8 = 1
  g9 = 1
  g10 = 1
  g11 = 1
  g12 = 1
  MsgBox "选择统计文件"
  With Application.FileDialog(msoFileDialogFolderPicker)
           If (.Show = -1) Then
            MyPath = .SelectedItems(1)
           End If
     End With
    If MyPath = "" Then
        Exit Sub
    End If
   MyPath = MyPath + "\"
   MyName = Dir(MyPath, vbDirectory)
Do While MyName <> ""
 If MyName <> "." And MyName <> ".." Then
     Open MyPath + MyName For Input As #1
         Do While Not EOF(1)
                   Line Input #1, mmlline
            If InStr(mmlline, "网元") > 0 Then
                   Line Input #1, mmlline
                   If InStr(mmlline, "CD-RNC") Then
                   GoTo qqq
                   End If
                   nodebname = Trim(Mid(mmlline, 2, 50))
                   Line Input #1, mmlline
                   Line Input #1, mmlline
              If InStr(mmlline, "网元断连") <= 0 And InStr(mmlline, "网元响应MML命令超时") <= 0 And InStr(mmlline, "没有权限") <= 0 Then
                   Line Input #1, mmlline
                   Line Input #1, mmlline
                   Line Input #1, mmlline
                   Line Input #1, mmlline
                   Line Input #1, mmlline
                              If InStr(mmlline, "没有查到相应的结果") > 0 Then
                              GoTo qqq
                              End If
                   Line Input #1, mmlline
                   Line Input #1, mmlline
               If InStr(mmlline, "=") <= 0 Then
                 If InStr(mmlline, "相关DSP号") > 0 Then
                   Line Input #1, mmlline
                   Line Input #1, mmlline
qiu:                    mmlline1 = mmlline
                             For j = 50 To 1 Step -1
                             mmlline1 = Replace(Trim(mmlline1), Application.Rept(" ", j), "|")
                             Next
                   a = Split(mmlline1, "|")
                   sig1(g1) = nodebname
                   sig2(g2) = a(0)
                   sig3(g3) = a(1)
                   sig4(g4) = a(2)
                   sig5(g5) = a(3)
                   sig6(g6) = a(4)
                   sig7(g7) = a(5)
                   sig8(g8) = a(6)
                   sig9(g9) = a(7)
                   sig10(g10) = a(8)
                   sig11(g11) = a(10)
                   sig12(g12) = a(11)
                   g1 = g1 + 1
                   g2 = g2 + 1
                   g3 = g3 + 1
                   g4 = g4 + 1
                   g5 = g5 + 1
                   g6 = g6 + 1
                   g7 = g7 + 1
                   g8 = g8 + 1
                   g9 = g9 + 1
                   g10 = g10 + 1
                   g11 = g11 + 1
                   g12 = g12 + 1
                   Line Input #1, mmlline
                                If InStr(mmlline, "结果个数") <= 0 Then
                                GoTo qiu
                                End If
              Else
                   Line Input #1, mmlline
                   Line Input #1, mmlline
qiu1:                    mmlline1 = mmlline
                   For j = 50 To 1 Step -1
                   mmlline1 = Replace(Trim(mmlline1), Application.Rept(" ", j), "|")
                   Next
                   a = Split(mmlline1, "|")
                   sig1(g1) = nodebname
                   sig2(g2) = a(0)
                   sig3(g3) = a(1)
                   sig4(g4) = a(2)
                   sig5(g5) = a(3)
                   sig6(g6) = a(4)
                   sig7(g7) = a(5)
                   sig8(g8) = a(6)
                   sig9(g9) = a(7)
                   sig10(g10) = a(8)
                   sig11(g11) = a(10)
                   sig12(g12) = a(11)
                   g1 = g1 + 1
                   g2 = g2 + 1
                   g3 = g3 + 1
                   g4 = g4 + 1
                   g5 = g5 + 1
                   g6 = g6 + 1
                   g7 = g7 + 1
                   g8 = g8 + 1
                   g9 = g9 + 1
                   g10 = g10 + 1
                   g11 = g11 + 1
                   g12 = g12 + 1
                   Line Input #1, mmlline
                   If InStr(mmlline, "结果个数") <= 0 Then
                   GoTo qiu1
                   End If
                End If

             Else
                   sig1(g1) = nodebname
                   sig2(g2) = Trim(Split(mmlline, "=")(1))
                    Line Input #1, mmlline
                   sig3(g3) = Trim(Split(mmlline, "=")(1))
                    Line Input #1, mmlline
                   sig4(g4) = Trim(Split(mmlline, "=")(1))
                    Line Input #1, mmlline
                   sig5(g5) = Trim(Split(mmlline, "=")(1))
                    Line Input #1, mmlline
                   sig6(g6) = Trim(Split(mmlline, "=")(1))
                    Line Input #1, mmlline
                   sig7(g7) = Trim(Split(mmlline, "=")(1))
                    Line Input #1, mmlline
                   sig8(g8) = Trim(Split(mmlline, "=")(1))
                    Line Input #1, mmlline
                   sig9(g9) = Trim(Split(mmlline, "=")(1))
                    Line Input #1, mmlline
                   sig10(g10) = Trim(Split(mmlline, "=")(1))
                    Line Input #1, mmlline
                   If Len(mmlline) > 12 Then
                    Line Input #1, mmlline
                   sig11(g11) = Trim(Split(mmlline, "=")(1))
                    Line Input #1, mmlline
                   sig12(g12) = Trim(Split(mmlline, "=")(1))
                   Else
                   sig11(g11) = Trim(Split(mmlline, "=")(1))
                    Line Input #1, mmlline
                   sig12(g12) = Trim(Split(mmlline, "=")(1))
                   End If
                   g1 = g1 + 1
                   g2 = g2 + 1
                   g3 = g3 + 1
                   g4 = g4 + 1
                   g5 = g5 + 1
                   g6 = g6 + 1
                   g7 = g7 + 1
                   g8 = g8 + 1
                   g9 = g9 + 1
                   g10 = g10 + 1
                   g11 = g11 + 1
                   g12 = g12 + 1
             End If
       End If
   End If

                   If InStr(mmlline, "仍有后续报告输出") > 0 Then
                      For w = 1 To 11
                      Line Input #1, mmlline
                      Next
                      gg = InStr(mmlline, "相关DSP号")
                       Line Input #1, mmlline
                       Line Input #1, mmlline
                    If gg > 0 Then
                     GoTo qiu
                    Else
                     GoTo qiu1
                    End If
                   End If
qqq:                Loop
                Close #1
 End If
     MyName = Dir
Loop
'//////////////////////////////////////////////////////////////////
  txtline = "站名" & "|" & "小区设备ID" & "|" & "载波资源ID" & "|" & "相关BBP槽号" & "|" & "相关DSP号" & "|" & "相关DSPGRP号" & "|" & "频点" & "|" & "载波服务类型" & "|" & "载波状态" & "|" & "载波类型" & "|" & "操作态" & "|" & "可用态" & vbCrLf
For i = 1 To g1
  txtline = txtline & sig1(i) & "|" & sig2(i) & "|" & sig3(i) & "|" & sig4(i) & "|" & sig5(i) & "|" & sig6(i) & "|" & sig7(i) & "|" & sig8(i) & "|" & sig9(i) & "|" & sig10(i) & "|" & sig11(i) & "|" & sig12(i) & vbCrLf
Next

Dim fso1 As Object
        Set fso1 = CreateObject("Scripting.FileSystemObject")
        Set Ts1 = fso1.CreateTextFile("D:\Q结果.txt")
        Ts1.WriteLine txtline
        Ts1.Close


    Workbooks.OpenText FileName:="D:\Q结果.txt", Origin:=936 _
        , StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
        ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=False _
        , Space:=False, Other:=True, OtherChar:="|", FieldInfo:=Array(Array(1, 1 _
        ), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1)), _
        TrailingMinusNumbers:=True
    ActiveWorkbook.SaveAs FileName:=mypathout + "基站载波统计结果" & Date & ".xlsx", _
        FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
    ActiveWindow.Close
    Kill "D:\q结果.txt"

 MsgBox "统计完成!结果保存在:" & mypathout & "基站载波统计结果" & Date & ".xlsx"
End Sub
2013-05-22 14:37
qbh555
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2012-12-26
收藏
得分:0 
回复 2楼 bczgvip
程序代码:
以下可以处理,就是速度非常的慢,请教快速处理方法。


Private Sub Command1_Click()
'/////////////////////////////////////////////////////////////////////////
  MsgBox "选择输出路径"
   With Application.FileDialog(msoFileDialogFolderPicker)
    If (.Show = -1) Then
        mypathout = .SelectedItems(1)
        End If
    End With
    If mypathout = "" Then
        Exit Sub
    End If
   mypathout = mypathout + "\"
'///////////////////////////////////////////////////////////////////////////
  Dim sig1(65535) As Variant
  Dim sig2(65535) As Variant
  Dim sig3(65535) As Variant
  Dim sig4(65535) As Variant
  Dim sig5(65535) As Variant
  Dim sig6(65535) As Variant
  Dim sig7(65535) As Variant
  Dim sig8(65535) As Variant
  Dim sig9(65535) As Variant
  Dim sig10(65535) As Variant
  Dim sig11(65535) As Variant
  Dim sig12(65535) As Variant
  g1 = 1
  g2 = 1
  g3 = 1
  g4 = 1
  g5 = 1
  g6 = 1
  g7 = 1
  g8 = 1
  g9 = 1
  g10 = 1
  g11 = 1
  g12 = 1
  MsgBox "选择统计文件"
  With Application.FileDialog(msoFileDialogFolderPicker)
           If (.Show = -1) Then
            MyPath = .SelectedItems(1)
           End If
     End With
    If MyPath = "" Then
        Exit Sub
    End If
   MyPath = MyPath + "\"
   MyName = Dir(MyPath, vbDirectory)
Do While MyName <> ""

 If MyName <> "." And MyName <> ".." Then
     Open MyPath + MyName For Input As #1
         Do While Not EOF(1)
                   Line Input #1, mmlline
            If InStr(mmlline, "网元") > 0 Then
                   Line Input #1, mmlline
                   If InStr(mmlline, "CD-RNC") Then
                   GoTo qqq
                   End If
                   nodebname = Trim(Mid(mmlline, 2, 50))
                   Line Input #1, mmlline
                   Line Input #1, mmlline
              If InStr(mmlline, "网元断连") <= 0 And InStr(mmlline, "网元响应MML命令超时") <= 0 And InStr(mmlline, "没有权限") <= 0 Then
                   Line Input #1, mmlline
                   Line Input #1, mmlline
                   Line Input #1, mmlline
                   Line Input #1, mmlline
                   Line Input #1, mmlline
                              If InStr(mmlline, "没有查到相应的结果") > 0 Then
                              GoTo qqq
                              End If
                   Line Input #1, mmlline
                   Line Input #1, mmlline
               If InStr(mmlline, "=") <= 0 Then
                 If InStr(mmlline, "相关DSP号") > 0 Then
                   Line Input #1, mmlline
                   Line Input #1, mmlline
qiu:                    mmlline1 = mmlline
                             For j = 50 To 1 Step -1
                             mmlline1 = Replace(Trim(mmlline1), Application.Rept(" ", j), "|")
                             Next
                   a = Split(mmlline1, "|")
                   sig1(g1) = nodebname
                   sig2(g2) = a(0)
                   sig3(g3) = a(1)
                   sig4(g4) = a(2)
                   sig5(g5) = a(3)
                   sig6(g6) = a(4)
                   sig7(g7) = a(5)
                   sig8(g8) = a(6)
                   sig9(g9) = a(7)
                   sig10(g10) = a(8)
                   sig11(g11) = a(10)
                   sig12(g12) = a(11)
                   g1 = g1 + 1
                   g2 = g2 + 1
                   g3 = g3 + 1
                   g4 = g4 + 1
                   g5 = g5 + 1
                   g6 = g6 + 1
                   g7 = g7 + 1
                   g8 = g8 + 1
                   g9 = g9 + 1
                   g10 = g10 + 1
                   g11 = g11 + 1
                   g12 = g12 + 1
                   Line Input #1, mmlline
                                If InStr(mmlline, "结果个数") <= 0 Then
                                GoTo qiu
                                End If
              Else
                   Line Input #1, mmlline
                   Line Input #1, mmlline
qiu1:                    mmlline1 = mmlline
                   For j = 50 To 1 Step -1
                   mmlline1 = Replace(Trim(mmlline1), Application.Rept(" ", j), "|")
                   Next
                   a = Split(mmlline1, "|")
                   sig1(g1) = nodebname
                   sig2(g2) = a(0)
                   sig3(g3) = a(1)
                   sig4(g4) = a(2)
                   sig5(g5) = a(3)
                   sig6(g6) = a(4)
                   sig7(g7) = a(5)
                   sig8(g8) = a(6)
                   sig9(g9) = a(7)
                   sig10(g10) = a(8)
                   sig11(g11) = a(10)
                   sig12(g12) = a(11)
                   g1 = g1 + 1
                   g2 = g2 + 1
                   g3 = g3 + 1
                   g4 = g4 + 1
                   g5 = g5 + 1
                   g6 = g6 + 1
                   g7 = g7 + 1
                   g8 = g8 + 1
                   g9 = g9 + 1
                   g10 = g10 + 1
                   g11 = g11 + 1
                   g12 = g12 + 1
                   Line Input #1, mmlline
                   If InStr(mmlline, "结果个数") <= 0 Then
                   GoTo qiu1
                   End If
                End If

             Else
                   sig1(g1) = nodebname
                   sig2(g2) = Trim(Split(mmlline, "=")(1))
                    Line Input #1, mmlline
                   sig3(g3) = Trim(Split(mmlline, "=")(1))
                    Line Input #1, mmlline
                   sig4(g4) = Trim(Split(mmlline, "=")(1))
                    Line Input #1, mmlline
                   sig5(g5) = Trim(Split(mmlline, "=")(1))
                    Line Input #1, mmlline
                   sig6(g6) = Trim(Split(mmlline, "=")(1))
                    Line Input #1, mmlline
                   sig7(g7) = Trim(Split(mmlline, "=")(1))
                    Line Input #1, mmlline
                   sig8(g8) = Trim(Split(mmlline, "=")(1))
                    Line Input #1, mmlline
                   sig9(g9) = Trim(Split(mmlline, "=")(1))
                    Line Input #1, mmlline
                   sig10(g10) = Trim(Split(mmlline, "=")(1))
                    Line Input #1, mmlline
                   If Len(mmlline) > 12 Then
                    Line Input #1, mmlline
                   sig11(g11) = Trim(Split(mmlline, "=")(1))
                    Line Input #1, mmlline
                   sig12(g12) = Trim(Split(mmlline, "=")(1))
                   Else
                   sig11(g11) = Trim(Split(mmlline, "=")(1))
                    Line Input #1, mmlline
                   sig12(g12) = Trim(Split(mmlline, "=")(1))
                   End If
                   g1 = g1 + 1
                   g2 = g2 + 1
                   g3 = g3 + 1
                   g4 = g4 + 1
                   g5 = g5 + 1
                   g6 = g6 + 1
                   g7 = g7 + 1
                   g8 = g8 + 1
                   g9 = g9 + 1
                   g10 = g10 + 1
                   g11 = g11 + 1
                   g12 = g12 + 1
             End If
       End If
   End If

                   If InStr(mmlline, "仍有后续报告输出") > 0 Then
                      For w = 1 To 11
                      Line Input #1, mmlline
                      Next
                      gg = InStr(mmlline, "相关DSP号")
                       Line Input #1, mmlline
                       Line Input #1, mmlline
                    If gg > 0 Then
                     GoTo qiu
                    Else
                     GoTo qiu1
                    End If
                   End If
qqq:                Loop
                Close #1

 End If
     MyName = Dir
Loop
'//////////////////////////////////////////////////////////////////
  txtline = "站名" & "|" & "小区设备ID" & "|" & "载波资源ID" & "|" & "相关BBP槽号" & "|" & "相关DSP号" & "|" & "相关DSPGRP号" & "|" & "频点" & "|" & "载波服务类型" & "|" & "载波状态" & "|" & "载波类型" & "|" & "操作态" & "|" & "可用态" & vbCrLf
For i = 1 To g1
  txtline = txtline & sig1(i) & "|" & sig2(i) & "|" & sig3(i) & "|" & sig4(i) & "|" & sig5(i) & "|" & sig6(i) & "|" & sig7(i) & "|" & sig8(i) & "|" & sig9(i) & "|" & sig10(i) & "|" & sig11(i) & "|" & sig12(i) & vbCrLf
Next

Dim fso1 As Object
        Set fso1 = CreateObject("Scripting.FileSystemObject")
        Set Ts1 = fso1.CreateTextFile("D:\Q结果.txt")
        Ts1.WriteLine txtline
        Ts1.Close


    Workbooks.OpenText FileName:="D:\Q结果.txt", Origin:=936 _
        , StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
        ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=False _
        , Space:=False, Other:=True, OtherChar:="|", FieldInfo:=Array(Array(1, 1 _
        ), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1)), _
        TrailingMinusNumbers:=True
    ActiveWorkbook.SaveAs FileName:=mypathout + "基站载波统计结果" & Date & ".xlsx", _
        FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
    ActiveWindow.Close
    Kill "D:\q结果.txt"


 MsgBox "统计完成!结果保存在:" & mypathout & "基站载波统计结果" & Date & ".xlsx"
End Sub
2013-05-22 14:40
qbh555
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2012-12-26
收藏
得分:0 
回复 10楼 风吹过b
谢谢,好了
2013-05-23 08:22
快速回复:请教:vb如何快速处理较大的txt文件?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.035431 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved