2021/12/23(Thu) 02:03:18 編集(投稿者)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim path As String = "D:\TEST\TestCSV.CSV" '対象ファイル
        Dim lines As List(Of String) = New List(Of String)
        Dim line As String
        Dim items() As String
        'ファイルをutf-8コードとして開く
        Using sr As New System.IO.StreamReader(path, System.Text.Encoding.GetEncoding("utf-8"))
            While sr.Peek() > -1
                line = sr.ReadLine()
                If line.Length > 0 Then
                    items = line.Split(",")
                    line = ""
                    For Each item As String In items
                        line &= "<td style=""white-space: nowrap;padding:0 5px;"">" & item & "</td>"
                    Next
                    line = "<tr>" & line & "</tr>"
                    lines.Add(line)
                End If
            End While
        End Using
        Dim sauce As String = ""
        sauce = "<table border=""0"" cellspacing=""0"" cellpadding=""0"" bordercolor=""#ffffff"">" & Strings.Join(lines.ToArray) & "</table>"
        sauce = "<html><body style=""margin:0;padding:0;"">" & sauce & "</body></html>"
        Dim webBrowserForPrinting As New WebBrowser()
        webBrowserForPrinting.Parent = Me
        webBrowserForPrinting.Visible = False
        AddHandler webBrowserForPrinting.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PrintDocument)
        webBrowserForPrinting.DocumentText = sauce
    End Sub
    Private Sub PrintDocument(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
        Dim webBrowserForPrinting As WebBrowser = CType(sender, WebBrowser)
        webBrowserForPrinting.ShowPrintPreviewDialog()
    End Sub