VB.NET 在文本文件(.txt)固定长度中导出datagridview

发布于 01-16 19:34 字数 2386 浏览 1 评论 0原文

我需要你的帮助。我需要将数据 gridview 导出到固定长度的文本 (.txt) 文件。

感谢您的帮助

这是我的代码:

Imports System.Data.OleDb

Imports System.IO

Public Class Form1
    
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    OpenFileDialog1.Filter = "Excel|`*`.xls;`*`.xlsx"
    OpenFileDialog1.ShowDialog()`
    txtFileName.Text = OpenFileDialog1.FileName
    Dim filepath As String = txtFileName.Text
    Dim connstring As String = String.Empty
    If filepath.EndsWith(".xls") Then
        connstring = String.Format("Provider=Microsoft.Jet.Oledb.4.0;" & "Data Source = {0};Extended Properties='Excel 8.0;HDR=yes'", filepath)
    Else
        connstring = String.Format("Provider=Microsoft.Ace.Oledb.12.0;" & "Data Source = {0};Extended Properties='Excel 8.0;HDR=yes'", filepath)
    End If
    Dim conn As New OleDbConnection(connstring)
    conn.Open()
    cboSheet.DataSource = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
    cboSheet.DisplayMember = "Table_Name"
    cboSheet.ValueMember = "Table_Name"
    conn.Close()

End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim filepath As String = txtFileName.Text
    Dim connstring As String = String.Empty
    If filepath.EndsWith(".xls") Then
        connstring = String.Format("Provider=Microsoft.Jet.Oledb.4.0;" & "Data Source = {0};Extended Properties='Excel 8.0;HDR=yes'", filepath)
    Else
        connstring = String.Format("Provider=Microsoft.Ace.Oledb.12.0;" & "Data Source = {0};Extended Properties='Excel 8.0;HDR=yes'", filepath)
    End If
    Dim cmd As New OleDbDataAdapter("Select * from[" & cboSheet.Text & "" & "]", connstring)
    cmd.TableMappings.Add("Table", "Table")
    Dim dt As New DataSet
    cmd.Fill(dt)
    dgProposte.DataSource = dt.Tables(0)
End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

Dim writer As TextWriter = New StreamWriter("C:\Users\antonio\Desktop\Prova.txt")

    For i As Integer = 0 To DataGridView1.Rows.Count - 2 Step +1
        For j As Integer = 0 To DataGridView1.Columns.Count - 1 Step +1
            writer.Write(vbTab & DataGridView1.Rows(i).Cells(j).Value.ToString() & vbTab & "")
        Next

        writer.WriteLine("")

    Next
    writer.Close()
    MessageBox.Show("Dati Esportati")

I need your help. I need to export my data gridview to a fixed length text (.txt) file.

Thank you for your help

This is my code:

Imports System.Data.OleDb

Imports System.IO

Public Class Form1
    
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    OpenFileDialog1.Filter = "Excel|`*`.xls;`*`.xlsx"
    OpenFileDialog1.ShowDialog()`
    txtFileName.Text = OpenFileDialog1.FileName
    Dim filepath As String = txtFileName.Text
    Dim connstring As String = String.Empty
    If filepath.EndsWith(".xls") Then
        connstring = String.Format("Provider=Microsoft.Jet.Oledb.4.0;" & "Data Source = {0};Extended Properties='Excel 8.0;HDR=yes'", filepath)
    Else
        connstring = String.Format("Provider=Microsoft.Ace.Oledb.12.0;" & "Data Source = {0};Extended Properties='Excel 8.0;HDR=yes'", filepath)
    End If
    Dim conn As New OleDbConnection(connstring)
    conn.Open()
    cboSheet.DataSource = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
    cboSheet.DisplayMember = "Table_Name"
    cboSheet.ValueMember = "Table_Name"
    conn.Close()

End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim filepath As String = txtFileName.Text
    Dim connstring As String = String.Empty
    If filepath.EndsWith(".xls") Then
        connstring = String.Format("Provider=Microsoft.Jet.Oledb.4.0;" & "Data Source = {0};Extended Properties='Excel 8.0;HDR=yes'", filepath)
    Else
        connstring = String.Format("Provider=Microsoft.Ace.Oledb.12.0;" & "Data Source = {0};Extended Properties='Excel 8.0;HDR=yes'", filepath)
    End If
    Dim cmd As New OleDbDataAdapter("Select * from[" & cboSheet.Text & "" & "]", connstring)
    cmd.TableMappings.Add("Table", "Table")
    Dim dt As New DataSet
    cmd.Fill(dt)
    dgProposte.DataSource = dt.Tables(0)
End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

Dim writer As TextWriter = New StreamWriter("C:\Users\antonio\Desktop\Prova.txt")

    For i As Integer = 0 To DataGridView1.Rows.Count - 2 Step +1
        For j As Integer = 0 To DataGridView1.Columns.Count - 1 Step +1
            writer.Write(vbTab & DataGridView1.Rows(i).Cells(j).Value.ToString() & vbTab & "")
        Next

        writer.WriteLine("")

    Next
    writer.Close()
    MessageBox.Show("Dati Esportati")

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

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

发布评论

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