Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CSV File

Status
Not open for further replies.

Keyth

Programmer
Feb 10, 2007
113
GB
I have created a CSV File using the following function:

Code:
    Private Function outputtocsv(ByVal tbl As DataTable) As String
        Dim returnString As String = AppPath() & "Exports\EndOfDay.csv"
        
        Dim outputtext As String = ""

        If File.Exists(returnString) Then
            File.Delete(returnString)
        End If


        Try
            Dim row As DataRowView
            Dim dv As DataView
            dv = tbl.DefaultView
            Dim headers As String = ""
            Dim clm As DataColumn
            For Each clm In dv.Table.Columns
                headers += clm.ColumnName & ", "
            Next
            Dim clmcount As Integer = tbl.Columns.Count
            headers = headers.Trim
            headers = Mid(headers, 1, headers.Length - 1)
            headers += ControlChars.CrLf
            Dim rowString As String = ""

            For Each row In dv
                rowString = ""
                For I As Integer = 0 To clmcount - 1
                    If row(I).ToString.Contains(CStr(ControlChars.Quote)) Then
                        Dim tempstr As String = row(I).ToString.Replace(CStr(ControlChars.Quote), CStr(ControlChars.Quote) & CStr(ControlChars.Quote))
                        tempstr = ControlChars.Quote & tempstr & ControlChars.Quote & ","
                        rowString += tempstr
                    ElseIf row(I).ToString.Contains(",") Then
                        rowString += ControlChars.Quote & row(I).ToString & ControlChars.Quote & ","
                    Else
                        rowString += row(I).ToString & ","
                    End If


                Next
                rowString = rowString.Trim
                rowString = Mid(rowString, 1, rowString.Length - 1)
                outputtext += rowString + ControlChars.CrLf
            Next

            My.Computer.FileSystem.WriteAllText(returnString, headers & outputtext, False)

        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
        Return returnString
    End Function

When I view the file using NOTEPAD the file looks fine, but when I open it in Excel, the first line start with the following characters 

Does anybody know why this happens? Is it the version of Excel that I am using? (MS Excel 2002)

Thanks Keyth.
 
At my side the code is working fine in text and excel. So check excel settings or any formats. I am using Excel 2k3.

Sharing the best from my side...

--Prashant--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top