Solved

Creating a tab delimited text file in VB.NET

Posted on 2011-03-02
5
2,573 Views
Last Modified: 2012-05-11
I have a datset with a single data table, and I would like to write the rows of data to a tab delimited text file.
Any source code would be great.
0
Comment
Question by:countrymeister
5 Comments
 
LVL 16

Accepted Solution

by:
Kalpesh Chhatrala earned 100 total points
ID: 35024040
DataTable2CSV - You can Save DataTable to Text File. you can change Seperator character as per your requirement.

Sample Code
http://www.devx.com/vb2themax/Tip/19703
0
 
LVL 13

Assisted Solution

by:gamarrojgq
gamarrojgq earned 50 total points
ID: 35024063
Hi,

Here is an example, you just have to add the

Imports System.IO

To your class
Dim dtTable As DataTable
        'Getting the Table from the Dataset
        dtTable = DataSet1.Tables(0)
        'Object that holds the File 
        Dim swrTXT As New StreamWriter("C:\TabDelimitedFile.txt")
        Dim drRow As DataRow
        Dim intCol As Int32
        Dim strLine As String
        'Delimiter Char
        Dim chrTAB As Char = Chr(Keys.Tab)

        'Loop all the rows in the table
        For Each drRow In dtTable.Rows
            strLine = ""
            'Loop all the columns to create the Line
            For intCol = 0 To dtTable.Columns.Count - 1
                'If is the last column do not add the TAB Delimiter
                If intCol = dtTable.Columns.Count - 1 Then
                    strLine &= drRow(intCol).ToString
                Else 'if not is the last column then ADD the TAB Delimiter
                    strLine &= drRow(intCol).ToString & chrTAB
                End If
            Next
            'Adding the line to the file
            swrTXT.WriteLine(strLine)
        Next

        'Saving the changes to the file
        swrTXT.Flush()
        swrTXT.Close()
        swrTXT.Dispose()

Open in new window

0
 
LVL 8

Assisted Solution

by:PagodNaUtak
PagodNaUtak earned 50 total points
ID: 35024079
Here is a sample:
If myReader.HasRows Then
  Do While myReader.Read()
    Console.WriteLine(vbTab & "{0}" & vbTab & "{1}", myReader.GetInt32(0), myReader.GetString(1))
  Loop
Else
  Console.WriteLine("No rows returned.")
End If

myReader.Close()
[C#]
if (myReader.HasRows)
  while (myReader.Read())
    Console.WriteLine("\t{0}\t{1}", myReader.GetInt32(0), myReader.GetString(1));
else
  Console.WriteLine("No rows returned.");

myReader.Close();

Open in new window

0
 
LVL 33

Assisted Solution

by:Todd Gerbert
Todd Gerbert earned 50 total points
ID: 35024121
Easy enough to just loop through the rows of the table and write the value of each column, plus a tab, to a text file.  I didn't bother to read that link above, so this may very well be the same thing. ;)

Imports System.Data
Imports System.IO

Module Module1

	Sub Main()
		'This sets up a dummy dataset with some
		'dummy data just for testing
		Dim table As New DataTable("testTable")
		table.Columns.Add("ID", Type.GetType("System.Int32"))
		table.Columns.Add("Kind", Type.GetType("System.String"))
		table.Columns.Add("Name", Type.GetType("System.String"))
		table.Rows.Add(1, "Animal", "Cat")
		table.Rows.Add(2, "Animal", "Dog")
		table.Rows.Add(3, "Vehicle", "Car")
		table.Rows.Add(4, "Vehicle", "Airplane")
		table.Rows.Add(5, "Vehicle", "Boat")
		table.Rows.Add(6, "Crayon", "Purple")
		table.Rows.Add(7, "Crayon", "Edible")

		Dim ds As New DataSet()
		ds.Tables.Add(table)

		ExportTable(ds.Tables("testTable"), "C:\test.txt")
	End Sub

	Sub ExportTable(ByVal table As DataTable, ByVal exportFile As String)
		Using exportStream As New StreamWriter(exportFile)
			Dim columncount = table.Columns.Count
			For Each r As DataRow In table.Rows
				For i As Integer = 0 To columncount - 2
					exportStream.Write("{0}{1}", r(i), vbTab)
				Next
				exportStream.WriteLine("{0}", r(columncount - 1))
			Next
		End Using
	End Sub
End Module

Open in new window

0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35024127
Whoa...I guess I must type slow!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now