Click here to Skip to main content
15,617,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
'create empty string
        Dim the_html_file As String = String.Empty

        Dim stylesheet As String = "  table.gridtable {margin:0 auto;width:95%;overflow:auto;font-family: helvetica,arial,sans-serif;"
        stylesheet &= "font-size:14px;color:#333333;border-width: 1px;border-color: #666666;border-collapse: collapse;text-align: center}"
        stylesheet &= "table.gridtable th {border-width: 1px;padding: 8px; border-style: solid;border-color: #666666;background-color: #F6B4A5;}"
        stylesheet &= "table.gridtable td {border-width: 1px;padding: 8px;border-style: solid;border-color: #666666;}"

        the_html_file = "<!DOCTYPE html><html><head><meta charset='UTF-8'><title>Cadet List</title><style>" & stylesheet & "</style></head><body>"

        the_html_file &= "<table class='gridtable'>"
        the_html_file &= "<thead><tr>"

        'get the column headers
        For Each column As DataGridViewColumn In grid.Columns
            the_html_file = the_html_file & "<th>" & column.HeaderText & "</th>"

        the_html_file = the_html_file & "</tr></thead><tbody>"

        'get the rows
        For Each row As DataGridViewRow In grid.Rows
            the_html_file &= "<tr>"
            'get the cells
            For Each cell As DataGridViewCell In row.Cells

                Dim cellcontent As String = cell.FormattedValue
                'replace < and > with html entities
                cellcontent = Replace(cellcontent, "<", "<")
                cellcontent = Replace(cellcontent, ">", ">")

                'using inline styles for column 1
                'If cell.ColumnIndex = 1 Then
                '    the_html_file = the_html_file & "<td style=color:white;background-color:red;font-weight:bold>" & cellcontent & "</td>"
                '    the_html_file = the_html_file & "<td>" & cellcontent & "</td>"
                'End If

                'no inline styles
                the_html_file = the_html_file & "<td>" & cellcontent & "</td>"

            the_html_file &= "</tr>"

        the_html_file &= "</tbody></table></body></html>"

        'write the file
        My.Computer.FileSystem.WriteAllText("C:\Users\staff\Documents\cadetlist.html", the_html_file, False)

        'pass file to default browser
        Dim pinfo As New ProcessStartInfo()
        pinfo.WindowStyle = ProcessWindowStyle.Normal
        pinfo.FileName = "C:\Users\staff\Documents\cadetlist.html"
        Dim p As Process = Process.Start(pinfo)

So here is my code all I want to do is save the file to the current users desktop or documents. I have searched everywhere and tried many solutions.

What I have tried:

I have tried to search online for the answer but nothing has worked.
Updated 3-Feb-23 7:49am

This CodeProject article Getting All "Special Folders" via VB.NET[^] will enable you to get the actual name of the folder you want to save to then use something like
My.Computer.FileSystem.WriteAllText(userDocFolder & "\cadetlist.html", the_html_file, False)
Share this answer
Maciej Los 19-Dec-19 7:25am    
CHill60 19-Dec-19 9:47am    
Thank you!
corporallawson 19-Dec-19 7:46am    
You Sir, are a saviour. Thank you very much.
CHill60 19-Dec-19 9:47am    
My pleasure!
file ///c /users/user/desktop/test page/yusif.html
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900