I have a .csv file which has data but not the column headers. I can able to create new .csv file with oldcsv file’s content. But I need to add column headers in first row and from second row the existing data should appear.
Here is the code I have written:
Dim ioFile As New System.IO.StreamReader("C:\sample.csv")
Dim ioLine As String Dim ioLines As String
ioLine = ioFile.ReadLine
ioLines = ioLine
While Not ioLine = ""
ioLine = ioFile.ReadLine
ioLines = ioLines & vbCrLf & ioLine
End While
Dim ioWriter As New System.IO.StreamWriter("C:\new.csv")
ioWriter.WriteLine(ioLines)
ioFile.Close()
ioWriter.Close()
Now i need to add column headers in new.csv as “ID”,”Name”,”Number”,”Amount”
You can set up the Reader and Writer to read and write within the loop and avoid creating a oiLines construct.
Check the code, as I wrote it blind. I am more C#, but the idea is sound. You just need to supply the inputFileName, outputFileName and headerLine. This also allows putting this in a method, where it is reuseable. 🙂