# write textbox strings to .csv and add fixed text to each row

Page 1 of 1

## 5 Replies - 5324 Views - Last Post: 30 August 2013 - 06:07 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=327946&amp;s=9eab3fda00b2fbfc36b7ad2091740b8d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 swjslj

Reputation: 0
• Posts: 7
• Joined: 29-August 13

# write textbox strings to .csv and add fixed text to each row

Posted 29 August 2013 - 05:54 PM

Hello, I'm hoping someone can give me some suggestions for what i thought was going to be a simple project, but quickly outstripped my very limited programming ability. I need to write a .csv in this format:

anything anything 1-712-222-3333 G3 0 0

This is an address book for a fax driver. The first two columns can be anything (just need something in them). Then three blank columns, the fax number, two blank columns, 0, four blank columns, 0

The app will have a multi-line textbox that the user can paste a list of fax numbers into, press a button and have the fax numbers written to a .csv . Each row (in excel) will be exactly the same except the fax number.

So far what I have is:

 Imports System
Imports System.IO
Imports System.Text

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim array() As String = TextBox1.Lines
Dim i As Integer = 0
array = TextBox1.Text.Split(" ")

For i = 0 To array.GetUpperBound(0)

File.WriteAllText("c:\test.csv", "." & ",")

File.AppendAllText("c:\test.csv", array(i))

Next i

End Sub


This gets me a .csv with all the fax numbers, but i have no idea how to add in the static text and spaces for each row.
Any suggestions will be hugely appreciated. Thanks,

Is This A Good Question/Topic? 0

## Replies To: write textbox strings to .csv and add fixed text to each row

### #2 _HAWK_

• Master(Of Foo)

Reputation: 1162
• Posts: 4,444
• Joined: 02-July 08

## Re: write textbox strings to .csv and add fixed text to each row

Posted 29 August 2013 - 06:40 PM

Are there multiple entries on diff lines in this textbox?

### #3 swjslj

Reputation: 0
• Posts: 7
• Joined: 29-August 13

## Re: write textbox strings to .csv and add fixed text to each row

Posted 29 August 2013 - 07:19 PM

_HAWK_, on 29 August 2013 - 06:40 PM, said:

Are there multiple entries on diff lines in this textbox?

The user will be copying columns of fax numbers from Excel and pasting them into the textbox; nothing else will be entered into the textbox. My difficulty is trying to find a way to add static columns of data to the array that holds the fax numbers. Thanks for the reply.

### #4 _HAWK_

• Master(Of Foo)

Reputation: 1162
• Posts: 4,444
• Joined: 02-July 08

## Re: write textbox strings to .csv and add fixed text to each row

Posted 29 August 2013 - 07:31 PM

Then you need to make a loop for each line in the textbox. Then save the data at one time. The ",,,," will add empty columns, place them where you need.

Dim emptyCols As String = ",,,,,"
Using sw As New StreamWriter({path})
'get each line
For Each line As String In textbox1.Text.Split(Environment.NewLine)
'replace the spaces with commas
Dim swLine As String = line.Replace(" ", ",") & emptyCols
sw.WriteLine(swLine)
Next
'close the stream
End Using


### #5 swjslj

Reputation: 0
• Posts: 7
• Joined: 29-August 13

## Re: write textbox strings to .csv and add fixed text to each row

Posted 29 August 2013 - 09:58 PM

You've got me much closer than I was, but I still seem to have a formatting problem. Here's how I tried changing your code to get the format I need:

 Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim emptyCols As String = ",,,,"
Using sw As New StreamWriter("c:\text.txt")
'get each line
For Each line As String In TextBox1.Text.Split(Environment.NewLine)
'replace the spaces with commas
Dim swLine As String = line.Replace(" ", ",") + emptyCols

sw.WriteLine("x," + "x," + ",,," + swLine + ",,," + "g3," + ",," + "0," + ",,,," + "0")

Next
'close the stream
End Using

End Sub


If I save the file as .txt and open in notepad it looks right, but when I open in Excel only the first line is correct. I'm completely confused. On the bright side though, you've got me much closer to a solution. Many thanks.

This post has been edited by andrewsw: 30 August 2013 - 06:11 AM
Reason for edit:: Removed previous quote

### #6 _HAWK_

• Master(Of Foo)

Reputation: 1162
• Posts: 4,444
• Joined: 02-July 08

## Re: write textbox strings to .csv and add fixed text to each row

Posted 30 August 2013 - 06:07 AM

Try saving it as .csv instead of .txt.