none
export data from table to TXT file RRS feed

  • Question

  • Hello all

    I have a problem with the form of witing data in a txt file.

    To clarify I want show what i have done

    Dim writer As New StreamWriter(f1)
    Dim tb As TextBox() = {Demand.TextBox1, Demand.TextBox2, Demand.TextBox3, Demand.TextBox4, Demand.TextBox5, Demand.TextBox6, Demand.TextBox7, Demand.TextBox8, Demand.TextBox9}
    
         
            f1.Position = 0
            For i = 0 To Form1.Table1.RowCount - 1
               writer.Write(Form1.Table1.Rows(i).HeaderCell.Value + " ") 'a1,a2...
            Next i
    
            
            f1.Position = 1
            For i = 0 To Form2.Table2.ColumnCount - 1
                writer.Write(Form2.Table2.Columns(i).Name + " ") 'b1,b2...
            Next i
    
    
            f1.Position = 2
            For i = 0 To 8
                If tb(i).Enabled = True Then
                    writer.WriteLine(tb(i).Text + " ") '10,20...
                End If
            Next
    
            writer.Close() 

    As a result I get this 

    My problem is that all the data is in one line and I would like each data type to be in a different line, like this:

    1st line : a1 a2..

    2nd line: b1 b2..

    3rd line: 10 20..

    I tried also to write "f1.position=0" in a line before "writer.write(..)" but it doesn't change anything

    And if i write "writer.writeline(..)" i get this:

    Thenks in advance

    Wednesday, November 22, 2017 1:09 PM

Answers

  • I did it this way: 

    (now it works, below is the results picture)

            For i = 0 To Form1.Table1.RowCount - 1
               writer.Write(Form1.Table1.Rows(i).HeaderCell.Value + " ") 'a1,a2...
            Next i
    
            writer.WriteLine("")
            
            For i = 0 To Form2.Table2.ColumnCount - 1
                writer.Write(Form2.Table2.Columns(i).Name + " ") 'b1,b2...
            Next i
    
            write.WriteLine("")
            
            For i = 0 To 8
                If tb(i).Enabled = True Then
                    writer.Write(tb(i).Text + " ") '10,20...
                End If
            Next
    
            writer.Close() 

    Result:

    


    • Marked as answer by Marziz Monday, November 27, 2017 8:44 AM
    • Edited by Marziz Monday, November 27, 2017 8:44 AM
    Monday, November 27, 2017 8:44 AM

All replies

  • Hi Marziz,

    According to your description, you want to export some data fro table into txt file, but I am not sure if you want to export entire table data or one column data into txt file, I do one example that export just one column data into txt file, please refer to:

     Dim dt As New DataTable
            dt.Columns.Add("Id", GetType(Integer))
            dt.Columns.Add("Column1", GetType(String))
            dt.Rows.Add(1, "a1")
            dt.Rows.Add(2, "b1")
            dt.Rows.Add(3, "a2")
            dt.Rows.Add(4, "b2")
            dt.Rows.Add(5, "10")
            dt.Rows.Add(6, "30")
    
            Dim list As New List(Of List(Of String))
            Dim list1 As New List(Of String)
            Dim list2 As New List(Of String)
            Dim list3 As New List(Of String)
            For i As Integer = 0 To dt.Rows.Count - 1
                Dim value As String = Convert.ToString(dt.Rows(i)("Column1")).Substring(0, 1)
                If value = "a" Then
                    list1.Add(dt.Rows(i)("Column1"))
    
                ElseIf value = "b" Then
                    list2.Add(dt.Rows(i)("Column1"))
                Else
                    list3.Add(dt.Rows(i)("Column1"))
                End If
            Next
            list.Add(list1)
            list.Add(list2)
            list.Add(list3)
            Dim path As String = "D:\test\Test1.txt"
            Dim writer As New StreamWriter(path)
            Try
                Dim sb As StringBuilder
                For i As Integer = 0 To list.Count - 1
                    sb = New StringBuilder
                    For j As Integer = 0 To list(i).Count - 1
                        sb.Append(list(i)(j))
                    Next
                    writer.WriteLine(sb.ToString())
                Next
    
            Catch ex As Exception
                Throw ex
            Finally
                If Not writer Is Nothing Then writer.Close()
            End Try

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 23, 2017 5:21 AM
    Moderator
  • I did it this way: 

    (now it works, below is the results picture)

            For i = 0 To Form1.Table1.RowCount - 1
               writer.Write(Form1.Table1.Rows(i).HeaderCell.Value + " ") 'a1,a2...
            Next i
    
            writer.WriteLine("")
            
            For i = 0 To Form2.Table2.ColumnCount - 1
                writer.Write(Form2.Table2.Columns(i).Name + " ") 'b1,b2...
            Next i
    
            write.WriteLine("")
            
            For i = 0 To 8
                If tb(i).Enabled = True Then
                    writer.Write(tb(i).Text + " ") '10,20...
                End If
            Next
    
            writer.Close() 

    Result:

    


    • Marked as answer by Marziz Monday, November 27, 2017 8:44 AM
    • Edited by Marziz Monday, November 27, 2017 8:44 AM
    Monday, November 27, 2017 8:44 AM