【解決方法】HTML ファイルを現在のユーザーのデスクトップに保存する


VB
'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>"
        Next

        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>"
                'Else
                '    the_html_file = the_html_file & "<td>" & cellcontent & "</td>"
                'End If

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

            Next
            the_html_file &= "</tr>"
        Next

        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)

したがって、ここに私のコードを示します。ファイルを現在のユーザーのデスクトップまたはドキュメントに保存するだけです。 私はどこでも検索し、多くの解決策を試しました。

私が試したこと:

答えをオンラインで検索しようとしましたが、何も機能しませんでした。

解決策 1

この CodeProject の記事 VB.NET経由ですべての「特別なフォルダ」を取得する[^] 保存したいフォルダの実際の名前を取得してから、次のようなものを使用できます

VB
My.Computer.FileSystem.WriteAllText(userDocFolder & "\cadetlist.html", the_html_file, False)

解決策 5

ファイル ///c /users/user/desktop/test page/yusif.html

コメント

タイトルとURLをコピーしました