Public Class ArinDv

    Private Sub DataGridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Select Case e.KeyCode
            Case Keys.O
                If e.Control Then
                    ExportView("c:\ExportData.txt", Me)
                End If
        End Select
    End Sub

    '//그리드뷰의 내용을 내보낸다(ArinDataView 의 일부)
    Public Sub ExportView(ByVal Filename As String, ByVal Dv As DataGridView)
        If Dv.RowCount < 1 Then
            MsgBox("저장가능한 목록이 없습니다", MsgBoxStyle.Information, "확인")
            Return
        End If
        Dim SD As New Windows.Forms.SaveFileDialog
        'SD.InitialDirectory = My.Application.Info.DirectoryPath & "\XLSOUT"
        SD.FileName = Filename
        ' If System.IO.Directory.Exists(SD.InitialDirectory) = False Then System.IO.Directory.CreateDirectory(SD.InitialDirectory)
        SD.Filter = "탭으로 분리된 텍스트파일(*.TXT)|*.TXT|콤마로 분리된 텍스트파일(*.CSV)|*.CSV"
        SD.FilterIndex = 0
        SD.RestoreDirectory = True
        If SD.ShowDialog <> Windows.Forms.DialogResult.OK Then Return
        SaveTextfile(Dv, SD.FileName, CChar(IIf(SD.FilterIndex = 2, ",", vbTab)))

    End Sub

    Private Sub SaveTextfile(ByVal Dv As DataGridView, ByVal FUllFileName As String, ByVal Splitter As Char)
        Dim Fs As New System.IO.FileStream(FUllFileName, IO.FileMode.Create)
        Dim SW As New System.IO.StreamWriter(Fs, System.Text.Encoding.Default)
        Dim Strbuf As String = ""
        SW.WriteLine("====")
        SW.WriteLine("파일생성일자" & vbTab & Now.ToShortDateString & vbTab & Now.ToShortTimeString)
        SW.WriteLine("====")
        For Each DC As Windows.Forms.DataGridViewColumn In Dv.Columns
            Strbuf = Strbuf & vbTab & DC.HeaderText
        Next
        SW.WriteLine(Strbuf)
        Strbuf = ""
        For Each DR As Windows.Forms.DataGridViewRow In Dv.Rows
            For i As Integer = 0 To Dv.Columns.Count - 1
                Strbuf = Strbuf & Splitter & DR.Cells(i).FormattedValue.ToString
            Next
            SW.WriteLine(Strbuf)
            Strbuf = ""
        Next
        SW.Dispose()
        Fs.Dispose()
        MsgBox("저장완료" & vbCrLf & FUllFileName, MsgBoxStyle.Information, "확인")
        Shell("notepad " & FUllFileName, AppWinStyle.NormalFocus)
    End Sub

End Class
