2 Replies - 14894 Views - Last Post: 21 June 2012 - 01:31 PM Rate Topic: -----

#1 .nethelp  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 88
  • Joined: 31-March 12

Write to csv file

Posted 21 June 2012 - 05:15 AM

Dear all i have to read from a text file( its the view source of an html document) and write them to an excel as csv i did the code and now its writing all the values( full name plus email under each other) to a text document but i cant seem to write to a csv file i tried to put .csv instead of txt but i dont know how to write columns the way i want it is full name will be under column NAME and email under column Email i dont need anything else please any help would be very appreciated here is the code i used.

ps. i dont want to add the extracted to it was just a test i just need the values name and email
Public Class Form1
    Dim i As Integer = 0
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim strSource As String() = File.ReadAllLines("c:\test.txt")
        For Each s As String In strSource
            ' Use StartsWith to find the opening tag.
            If s.Trim().StartsWith("<strong>") Then
                ' Locate the end of opening tab.
                Dim openTagEndPosition As Integer = s.IndexOf("<strong>")
                If openTagEndPosition >= 0 Then
                    s = s.Substring(openTagEndPosition + 8).Split("<")(0)
                    Dim objwri As New StreamWriter("c:\test1.txt", True)
                    objwri.WriteLine("Email: " + s)
                    objwri.Close()
                End If
            Else
                If s.Trim.Contains("Full Name :") Then
                    i += 1
                    Dim opentagendpositionE As Integer = s.IndexOf("Full Name :")
                    s = s.Substring(opentagendpositionE + 12).Split("<")(0)
                    Dim objwri As New StreamWriter("c:\test1.txt", True)
                    objwri.WriteLine(i & "-" & "Full Name: " + s)
                    objwri.Close()
                End If
            End If
        Next
        Dim objwrii As New StreamWriter("c:\test1.txt", True)

        objwrii.WriteLine()
        objwrii.WriteLine(DateTime.Now)
        objwrii.WriteLine("=============================================")
        objwrii.WriteLine("Extracted By " & TextBox1.Text & "")
        objwrii.Close()
        Dim strSource1 As String() = File.ReadAllLines("c:\test1.txt")
        MessageBox.Show(strSource1.Count.ToString)
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If TextBox1.Text = String.Empty Then
            Button1.Enabled = False
            If TextBox1.Text <> "" Then
                Button1.Enabled = True
            End If
        End If
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If TextBox1.Text = String.Empty Then
            Button1.Enabled = False
        Else
            Button1.Enabled = True
        End If
    End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: Write to csv file

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5519
  • View blog
  • Posts: 11,827
  • Joined: 02-June 10

Re: Write to csv file

Posted 21 June 2012 - 07:31 AM

Its a lot more than just changing the extension. :yes:

You have a LOT of reading to do. Best of luck on this project.
http://msdn.microsof...l(v=vs.80).aspx
Was This Post Helpful? 0
  • +
  • -

#3 .nethelp  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 88
  • Joined: 31-March 12

Re: Write to csv file

Posted 21 June 2012 - 01:31 PM

obviously you were wrong it worked fine
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim strSource As String() = File.ReadAllLines("c:\test.txt")
        Dim objfirst As New StreamWriter("c:\test1.csv")
        objfirst.Write("name" + "," + "email")
        objfirst.WriteLine()
        objfirst.Close()
        For Each s As String In strSource
            ' Use StartsWith to find the opening tag.
            If s.Trim().StartsWith("<strong>") Then
                ' Locate the end of opening tab.
                Dim openTagEndPosition As Integer = s.IndexOf("<strong>")
                If openTagEndPosition >= 0 Then
                    s = s.Substring(openTagEndPosition + 8).Split("<")(0)
                    Dim objwri As New StreamWriter("c:\test1.csv", True)
                    objwri.WriteLine(s)
                    objwri.Close()
                End If
            Else
                If s.Trim.Contains("Full Name :") Then
                    i += 1
                    Dim opentagendpositionE As Integer = s.IndexOf("Full Name :")
                    s = s.Substring(opentagendpositionE + 12).Split("<")(0)
                    Dim objwri As New StreamWriter("c:\test1.csv", True)
                    objwri.Write(s + ",")
                    objwri.Close()
                End If
            End If
        Next


        
    End Sub



Thank you
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1