有没有办法将文本文件导入vb.net中的datagridview

发布于 2025-01-26 00:30:53 字数 956 浏览 4 评论 0原文

美好的一天,我将文本文件导入DataGridView,我想拥有这样的输出

|名称|仪表编号

0123RR Road 6 | Doe,John | 123ABC456789

文本文件看起来像是此

0123RR ROAD 6 DOE,JOHN 123ABC456789

地址的字符数量为25和10,从名称到仪表编号。

我的代码看起来像这样,

    Dim lines() As String
    Dim vals() As String
    OpenFileDialog1.Filter = "TEXT FILE | *.txt |ALL FILES |*.*"
    If OpenFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
        lines = File.ReadAllLines(OpenFileDialog1.FileName)
        For i As Integer = 12 To lines.Length - 1 Step +1

            'Dim newlines As String = lines(i).Substring(25, 20)

            vals = lines(i).ToString().Split(" "c, StringSplitOptions.RemoveEmptyEntries)


            Dim row(vals.Length - 1) As String


            For j As Integer = 0 To vals.Length - 1 Step 2

                row(j) = vals(j).Trim()


            Next j
            dt.Rows.Add(row)


        Next i
    End If

谢谢

Good day, i am importing text file to datagridview and i want to have the output like this

LOT ADDRESS | NAME | METER NUMBER

0123RR ROAD 6 | DOE, JOHN | 123ABC456789

text file looks like this

0123RR ROAD 6 DOE, JOHN 123ABC456789

the character count from address to name is 25 and 10 from name to meter number.

my code looks like this

    Dim lines() As String
    Dim vals() As String
    OpenFileDialog1.Filter = "TEXT FILE | *.txt |ALL FILES |*.*"
    If OpenFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
        lines = File.ReadAllLines(OpenFileDialog1.FileName)
        For i As Integer = 12 To lines.Length - 1 Step +1

            'Dim newlines As String = lines(i).Substring(25, 20)

            vals = lines(i).ToString().Split(" "c, StringSplitOptions.RemoveEmptyEntries)


            Dim row(vals.Length - 1) As String


            For j As Integer = 0 To vals.Length - 1 Step 2

                row(j) = vals(j).Trim()


            Next j
            dt.Rows.Add(row)


        Next i
    End If

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文