1

我有一个带有 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 和 MTB 开头的每一行,值出现在“=”符号之后。例如从 H'C350 .... 到 H'0000。然后调用 excel 并将每个块的值放在一行中。

请帮助解决这个问题。

4

2 回答 2

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
于 2013-07-15T12:32:27.077 回答
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)
于 2013-07-15T12:20:33.137 回答