VB从文本文件中读取,搜索文本并输入excel

时间:2013-07-15 12:12:49

标签: vb.net

我有一个带有RichTextBox的表单,其中显示了输入文件的路径。我的表格中也有一个按钮。输入文本文件是一个大文件,重复以下数据块:

        PROGRAM TRACING   (1)

    ON IA
    MPAG    (B) AR0=H'0000 7402         
    MTV     (B) VAR H'01 62AE:H'148=H'C350
    MTV     (B) VAR H'01 62AE:H'147=H'071F
    MTV     (B) VAR H'01 62AE:H'02B=H'4404
    MTV     (B) VAR H'01 62AE:H'02C=H'5712
    MTV     (B) VAR H'01 62AE:H'029=H'5405
    MTV     (B) VAR H'01 62AE:H'02A=H'F272
    MTV     (B) VAR H'01 62AE:H'093=H'3853
    MTV     (B) VAR H'01 62AE:H'094=H'2060
    MTV     (B) VAR H'01 62AE:H'091=H'3046
    MTV     (B) VAR H'01 62AE:H'092=H'F024
    MTV     (B) VAR H'01 62AE:H'1BB=H'0D07
    MTV     (B) VAR H'01 62AE:H'1BC=H'0B0B
    MTV     (B) VAR H'01 62AE:H'1BD=H'0616
    MTB     (B) VAR H'00 152E:H'00B(H'0001)=H'0079
    MTB     (B) VAR H'00 152E:H'00B(H'0002)=H'0019
    MTB     (B) VAR H'00 152E:H'00B(H'0003)=H'0062
    MTB     (B) VAR H'00 152E:H'00B(H'0004)=H'0086
    MTB     (B) VAR H'00 152E:H'00B(H'0005)=H'0041
    MTB     (B) VAR H'00 152E:H'00B(H'0006)=H'0000
    MTB     (B) VAR H'00 152E:H'00B(H'0007)=H'0000
    MBPAG   (B) VAR H'027=H'5970 C25F

END

我需要的是:当点击按钮时,在文件中搜索以MTV& amp;开头的每一行。 MTB,出现在“=”符号后面的值。例如,从H'C350开始......到H'0000。然后打电话给excel&为每个块提供单行的值。

请帮忙解决这个问题。

2 个答案:

答案 0 :(得分:1)

完整LinqPad - 准备好示例:

Sub Main
    ' example data
    Dim data = <xml>
       PROGRAM TRACING   (1)

    ON IA
    MPAG    (B) AR0=H'0000 7402         
    MTV     (B) VAR H'01 62AE:H'148=H'C350
    MTV     (B) VAR H'01 62AE:H'147=H'071F
    MTV     (B) VAR H'01 62AE:H'02B=H'4404
    MTV     (B) VAR H'01 62AE:H'02C=H'5712
    MTV     (B) VAR H'01 62AE:H'029=H'5405
    MTV     (B) VAR H'01 62AE:H'02A=H'F272
    MTV     (B) VAR H'01 62AE:H'093=H'3853
    MTV     (B) VAR H'01 62AE:H'094=H'2060
    MTV     (B) VAR H'01 62AE:H'091=H'3046
    MTV     (B) VAR H'01 62AE:H'092=H'F024
    MTV     (B) VAR H'01 62AE:H'1BB=H'0D07
    MTV     (B) VAR H'01 62AE:H'1BC=H'0B0B
    MTV     (B) VAR H'01 62AE:H'1BD=H'0616
    MTB     (B) VAR H'00 152E:H'00B(H'0001)=H'0079
    MTB     (B) VAR H'00 152E:H'00B(H'0002)=H'0019
    MTB     (B) VAR H'00 152E:H'00B(H'0003)=H'0062
    MTB     (B) VAR H'00 152E:H'00B(H'0004)=H'0086
    MTB     (B) VAR H'00 152E:H'00B(H'0005)=H'0041
    MTB     (B) VAR H'00 152E:H'00B(H'0006)=H'0000
    MTB     (B) VAR H'00 152E:H'00B(H'0007)=H'0000
    MBPAG   (B) VAR H'027=H'5970 C25F

END
    </xml>

    ' create an excel instance
    Dim xl = Microsoft.VisualBasic.CreateObject("Excel.Application")
    xl.Visible = True
    Dim wb = xl.Workbooks.Add()
    Dim sheet = wb.ActiveSheet

    ' find lines starting with any whitepace followed by MTV or MTB and capture
    ' the text after =
    Dim pattern = "(?<=\s*(MTV|MTB).*=).*"

    Dim i = 1
    Dim arg = { Microsoft.VisualBasic.ControlChars.CrLf,  Microsoft.VisualBasic.ControlChars.Lf }

    ' You want to use File.ReadLines('yourfile.txt') here instead of data.Value.Split...
    For Each line in data.Value.Split(arg, StringSplitOptions.None)
        Dim match = Regex.Match(line, pattern)
        ' check each line and fill sheet
        If match.Success Then
            sheet.Cells(i, 1).Value = match.Value
            i += 1
        End If
    Next 
End Sub

答案 1 :(得分:0)

由于我不知道你是如何创建excel文件的(EPPlus,Interop等),我向你展示了一种在=之后得到所有子串的方法:

Dim allData = From line In File.ReadLines("Your Path")
              Where line.TrimStart.StartsWith("MTV") OrElse line.TrimStart.StartsWith("MTB")
              Let index = line.LastIndexOf("=")
              Where index >= 0
              Select line.Substring(index + 1)