Solved

Creating a tab delimited text file in VB.NET

Posted on 2011-03-02
5
3,065 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

726 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