How to create csv file from string variable using streamwriter in Vb.Net?

I am working on VS 2012, Vb.Net using .Net 4.0.

Using Streamwriter or FileStream, I am trying to create a Temp csv that will be in the c: \ windows \ temp folder. Values โ€‹โ€‹for the CSV file will be populated from the string variable.

Examples of values โ€‹โ€‹in a string variable will look like this:

1,2,3,411,345 2,3,4,55,678 5,6,7,8,9999 

How to write a CSV file from a string variable?

 Dim reader As StreamReader = New System.IO.StreamReader(File.OpenRead("D:\CSV\Test.csv")) Dim listA As New List(Of String)() Dim listB As New List(Of String)() Dim s As String = "" While Not reader.EndOfStream Dim line As String = reader.ReadLine() Dim values As String() = line.Split(";"c) listA.Add(values(0)) s = s + line + Chr(10) End While 
+5
source share
1 answer

Using Streamwriter (sw - variable declaration). I can write a .csv file.

  Public Sub Test() Try Dim reader As StreamReader = New System.IO.StreamReader(File.OpenRead("D:\CSV\Test.csv")) Dim listA As New List(Of String)() If File.Exists("d:\CSV\TestOut.csv") Then File.Delete("d:\CSV\TestOut.csv") End If Dim sw As New StreamWriter("d:\CSV\TestOut.csv") Dim s As String = String.Empty While reader.Peek() >= 0 Dim line As String = reader.ReadLine() Dim values As String() = line.Split(";"c) listA.Add(values(0)) s = s + line + Chr(10) End While reader.Close() sw.Write(s) sw.Close() Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub 
+3
source

Source: https://habr.com/ru/post/1203207/


All Articles