2013-07-15 85 views
1

我有一個RichTextBox窗體,其中顯示輸入文件的路徑。我在表格中也有一個按鈕。輸入文本文件是一個大的文件,下面的數據塊的重複:VB從文本文件中讀取,搜索文本並饋送到excel

 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 &爲每個塊提供單行的值。

請幫忙。

回答

1

完全LinqPad -ready例如:

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 
0

,因爲我不知道你是如何創建的Excel文件(EPPlus,互操作等),我告訴你一個辦法後=獲得的所有子:

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)