troubleshooting Question

is there a way to format a .CSV file created with a vb.net app?

Avatar of savache27
savache27 asked on
Visual Basic.NET
3 Comments1 Solution255 ViewsLast Modified:
Hi,

I'm trying to find out if there is a way to format a .CSV file created with a vb.net app. The code works in that it writes the file, but I would like to have the Headers in bold and I'm not sure if that can be done, or if so how to do it. I'll include my code. If someone could look over it and tell me if there is a way to do this I would really appreciate it. Thanks!
'File Header line
        vHTML = vHTML.Append("Department,")
        vHTML = vHTML.Append("Course,")
        vHTML = vHTML.Append("Term,")
        vHTML = vHTML.Append("Author,")
        vHTML = vHTML.Append("ISBN,")
        vHTML = vHTML.Append("Title,")
        vHTML = vHTML.Append("Edition,")
        vHTML = vHTML.Append("Publisher,")
        vHTML = vHTML.Append("Publication Date,")
        vHTML = vHTML.Append("Is New,")
        vHTML = vHTML.Append(vbCrLf)
 
        Myconn.Open()
        Mycomm = New SqlCommand(s, Myconn)
        Try
            dtr = Mycomm.ExecuteReader()
            If dtr.HasRows Then
                Success = True
                While dtr.Read
                    Dim vDept, vCourse, vTerm, vAuthor, vISBN, vTitle, vEditor, vPublisher, vPubDate, vIsNew
                    vDept = Replace(dtr("Department"), "''", "'")
                    vDept = Replace(vDept, ",", "")
                    vCourse = Replace(dtr("Course"), "''", "'")
                    vCourse = Replace(vCourse, ",", "")
                    vTerm = Replace(dtr("Term"), "''", "'")
                    vTerm = Replace(vTerm, ",", "")
                    vAuthor = Replace(dtr("Author"), "''", "'")
                    vAuthor = Replace(vAuthor, ",", "")
                    vISBN = Replace(dtr("ISBN"), "''", "'")
                    vISBN = Replace(vISBN, ",", "")
                    vTitle = Replace(dtr("Title"), "''", "'")
                    vTitle = Replace(vTitle, ",", "")
                    vEditor = Replace(dtr("Editor"), "''", "'")
                    vEditor = Replace(vEditor, ",", "")
                    vPublisher = Replace(dtr("Publisher"), "''", "'")
                    vPublisher = Replace(vPublisher, ",", "")
                    vPubDate = Replace(dtr("PublicationDate"), "''", "'")
                    vPubDate = Replace(vPubDate, ",", "")
                    vIsNew = Replace(dtr("IsNew"), "''", "'")
                    vIsNew = Replace(vIsNew, ",", "")
 
                    vHTML = vHTML.Append(Trim(vDept))
                    vHTML = vHTML.Append(",")
                    vHTML = vHTML.Append(Trim(vCourse))
                    vHTML = vHTML.Append(",")
                    vHTML = vHTML.Append(Trim(vTerm))
                    vHTML = vHTML.Append(",")
                    vHTML = vHTML.Append(Trim(vAuthor))
                    vHTML = vHTML.Append(",=""")
                    vHTML = vHTML.Append(Trim(vISBN))
                    vHTML = vHTML.Append(""",")
                    vHTML = vHTML.Append(Trim(vTitle))
                    vHTML = vHTML.Append(",")
                    vHTML = vHTML.Append(Trim(vEditor))
                    vHTML = vHTML.Append(",")
                    vHTML = vHTML.Append(Trim(vPublisher))
                    vHTML = vHTML.Append(",")
                    vHTML = vHTML.Append(Trim(vPubDate))
                    vHTML = vHTML.Append(",")
                    vHTML = vHTML.Append(Trim(vIsNew))
                    vHTML = vHTML.Append(",")
                    vHTML = vHTML.Append(vbCrLf)
                End While
            Else
                Success = False
                Myconn.Close()
                lblLink.Text = "No Results Found"
            End If
        Catch ex As Exception
            Success = False
            'Response.Write(sql & "<br>")
            Response.Write(ex.Message & "<br>")
        End Try
        dtr.Close()
        Myconn.Close()
        Dim fileExcel As String
        Dim nRandom As Random = New Random(DateTime.Now.Millisecond)
        fileExcel = "t" & nRandom.Next().ToString()
        Dim vFileName = "ExcelFiles/" & fileExcel & "_books.csv"
        Try
            Dim FILENAME As String = Server.MapPath(vFileName)
            Dim objStreamWriter As StreamWriter
            objStreamWriter = File.CreateText(FILENAME)
            objStreamWriter.WriteLine(vHTML)
            objStreamWriter.Close()
            lblLink.Visible = True
            lblLink.Text = "Excel file located <a href='" & vFileName & "'target=""_blank"">here</a>."
        Catch
            'Response.Write("<p><strong>An Error Occurred:</strong> " & ex.Message & "</p>" & vbCrLf)
            'If you get an error, make sure the directory has write permissions!
            lblStatus.Visible = True
            lblStatus.Text = "An error occurred attempting to produce the Excel file."
        End Try
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros