winforms 如何在.vb中将数据从文本文件解析为datagridview

xytpbqjk  于 2022-11-25  发布在  其他
关注(0)|答案(1)|浏览(124)

我想在VB.NET中解析从记事本到datagridview的数据,我使用分隔符空格(““)来拆分字符串,但是如果其中一个字段为空,它将忽略空格并合并到该字段的预定空格中。任何人都可以调整这些代码吗?
我的代码:

OpenFileDialog1.ShowDialog()

Try
    Dim Delimiter As Char = " "
    Dim Datagrid As New DataTable
    Dim dr As DataRow
    
    Datagrid.Columns.Add("SER NO")
    Datagrid.Columns.Add("MANGU TANAKA")
    Datagrid.Columns.Add("ILHANA")
    Datagrid.Columns.Add("TAOYA")            
    Datagrid.Columns.Add("SUN NM")
    Datagrid.Columns.Add("T/N")
    Datagrid.Columns.Add("IDD NUMBER")
    Datagrid.Columns.Add("TOTAL IDD")
    Datagrid.Columns.Add("TIX")
    Datagrid.Columns.Add("PAY")
    Datagrid.Columns.Add("DISC")
    Datagrid.Columns.Add("FO")
    Datagrid.Columns.Add("CUS NAME")
    Datagrid.Columns.Add("ID")
    Datagrid.Columns.Add("REFN")
    Datagrid.Columns.Add("TRNX")           

    dr = Datagrid.NewRow()
    Dim Data_Read As New System.IO.StreamReader(OpenFileDialog1.FileName)
    While Data_Read.Peek() <> -1
        Datagrid.Rows.Add(Data_Read.ReadLine().Split(Delimiter))
    End While
    DataGridView1.DataSource = Datagrid
    Call Datagrid_Row_Count()
Catch ex As Exception
    MsgBox(ex.Message, MsgBoxStyle.Critical, "Import")
End Try

数据来源:

SER NO - 12345678              MANGU TANAKA 20OCT                  PERANILA DEF
  ILHANA - ABCYY1234             PILIANMON:
  TAOYA  - ABC                                                        20 OCT 2022
  -------------------------------------------------------------------------------
  SUN NM T/N IDD NUMBER TOTAL IDD    TIX    PAY   DISC FO CUS NAME ID REFN   TRNX
  -------------------------------------------------------------------------------
  432149 000 5123456766    59.300 23.300  0.000  0.000*CR ABC/EFGH AA P8CBCK CONF
  432150 111 6123456767    43.450 18.450  0.000  0.000    ABCDEFGH QQ P9B26B BALK
  432151 222 7123456768    53.000 42.000  0.000  0.000 CR ABCDE/GH BB P9TSDX CONF
  432152 000 8123456769    88.750 25.750  0.000  0.000    ABC/EFGH AA P9YGTH BALK
  432153 333 9123456770    88.100 52.100  0.000  0.000*CR ABCDEFGH XX P9IQ27 CONF
  432154 555 1123456771   313.800 92.800  0.000  0.000 CR ABC/EFGH AA PCPZPH CONF
  From: Please do not respond <abc@noreply.net.com>
  Sent: Thursday, October 20, 2022 9:05 AM
  To:   REPLYME@SAMPLES.COM
  Subject:  Your persnl information

  SER NO - 87654321              MANGU TANAKA 20OCT-21OCT            PERANILA XYZ
  ILHANA - XYZWE5678             PILIANMON:
  TAOYA  - 1234AB                                                     20 OCT 2022
  -------------------------------------------------------------------------------
  SUN NM T/N IDD NUMBER TOTAL IDD    TIX    PAY   DISC FO CUS NAME ID REFN   TRNX
 -------------------------------------------------------------------------------
  123412*111 9875601128    81.150 23.150  0.000  0.000 CR ABCDEFGH AA M7NBVG CONF
  123413*111 9875601129    81.150 23.150  0.000  0.000 CR ABCD/FGH 11 M7NBVG CONF
  123414*222 9875601130    65.150 21.150  0.000  0.000 CR ABCDEFGH AA M7NBVG CONF
  123415*555 9875601131   168.200 20.200  0.000  0.000 CR AB/DEFGH 22 M7ZBCJ CONF
  123416*666 9875601132   168.200 20.200  0.000  0.000*CR ABCDEFGH AA M7ZBCJ BALK
  123417*777 9875601133    63.400 46.400  0.000  0.000 CR ABC/EFGH BB MQTEP7 CONF
  123418*012 9875601134    62.200 17.200  0.000  0.000    ABCDEFGH 11 MAUVAF CONF
  123419*023 9875601135    61.750 35.750  0.000  0.000 CR ABC/EFGH AA MQOEDC CONF
  123420*055 9875601136   285.600 100.6T  0.000  0.000    ABCDEFGH 55 ABPCOX BALK
  123421*009 9875601137    43.600 27.600  0.000  0.000*CR ABCDE/GH 00 MKQWEV CONF
  From: Please do not respond <abc@noreply.net.com>
  Sent: Thursday, October 20, 2022 9:05 AM
  To:   REPLYME@SAMPLES.COM
  Subject:  Your persnl information

  SER NO - 23456789              MANGU TANAKA 19OCT                  PERANILA DEF
  ILHANA - WERQQ7895             PILIANMON:
  TAOYA  - 1111ZZ                                                     20 OCT 2022
  -------------------------------------------------------------------------------
  SUN NM T/N IDD NUMBER TOTAL IDD    TIX    PAY   DISC FO CUS NAME ID REFN   TRNX
  -------------------------------------------------------------------------------
  987505*123 9870290897    51.650 26.650  0.000  0.000 CR ABCDEFG/ QQ M6XYZL CONF
  987506*321 9870290898    51.650 26.650  0.000  0.000*   ABCDEF/H AA M6XYZL BALK
  987507*567 9870290899   156.400 102.4T  0.000  0.000 CR ABC/EFGH ZZ JDAWEF NOTC
  From: Please do not respond <abc@noreply.net.com>
  Sent: Thursday, October 20, 2022 9:05 AM
  To:   REPLYME@SAMPLES.COM
  Subject:  Your persnl information

  SER NO - 98765432              MANGU TANAKA 19OCT-20OCT            PERANILA EFG
  ILHANA - ZXCQA0123             PILIANMON:
  TAOYA  - ABCD11                                                     20 OCT 2022
  -------------------------------------------------------------------------------
  SUN NM T/N IDD NUMBER TOTAL IDD    TIX    PAY   DISC FO CUS NAME ID REFN   TRNX
  -------------------------------------------------------------------------------
  987593 890 9870290968    60.100 35.100  0.000  0.000    AB/DEFGH 12 OOQW7H CONF
  987594*098 9870290897    51.650 26.650  0.000  0.000*CR ABCD/FGH 55 MQZVZL BALK

预期结果

4smxwvx5

4smxwvx51#

伪代码。...下面添加了VB。不过,这是假设没有头或数据中有空格

int headersplitCount = 0;
DataTable data = null;

string[] lines = file.ReadAllLines(fileName);
foreach(string line in lines) // iterate every line
{
    if (headersplitCount == 0)
    {    
        if (line.StatrtsWith("---"))  // look for the first header split
            headersplitCount++;
        continue; // go next line
    }
    else if (headersplitCount == 1)  // you have one split already, look for header row
    {
        if (!line.StatrtsWith("---")) // not second split - this is header row
        {
            if (data == null) // check table
            {
                data = new DataTable(); // create columns from header
                string[] headers = line.Split(' ');
                foreach(string header in headers)
                    data.Columns.Add(header, typeof(string)); // <-- check syntax
                    
            }
        }
        else
            headersplitCount++; // make it 2

        continue;
    }
    else if (headersplitCount == 2) // you have reached pasesd header
    {
        if (line.StartsWith("From: ")) // end of data
            headersplitCount = 0; // reset
        else 
        {
            data.Rows.Add(line.Split(' '));
        }
    }

}

此时,您的DataTable已经填充了数据。现在,将它绑定到数据网格,就可以开始了。
VB语言

dim headersplitCount as integer = 0
dim data as DataTable = nothing

dim lines() as string = file.ReadAllLines(fileName)
for each(line as string in lines) ' iterate every line

    if (headersplitCount = 0) then
       
        if (line.StatrtsWith("---")) then  ' look for the first header split
            headersplitCount += 1
        end if
        continue ' go next line
    
    elseif (headersplitCount = 1) then  ' you have one split already, look for header row
    
        if (not line.StatrtsWith("---")) then ' not second split - this is header row
        
            if (data is nothing) then ' check table
            
                data = new DataTable() ' create columns from header
                dim headers() as string = line.Split(" "c)
                for each(header as string in headers)
                    data.Columns.Add(header, GetType(string)) ' <-- check syntax
                next
            end if
        
        else
            headersplitCount += 1 ' make it 2
        end if
        continue
    
    elseif (headersplitCount = 2) then ' you have reached pasesd header
    
        if (line.StartsWith("From: ")) then ' end of data
            headersplitCount = 0 ' reset
        else  
            data.Rows.Add(line.Split(' ')) ' add data
        end if
    end if

next

相关问题