Datagridview Data Export to Excel In Vb.Net with Save File Dialog | Visual Studio Code

3

Imports System.IO
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
                SaveFileDialog1.Filter = "Excel Documents|*.xlsx"
    End Sub
    Public Function ExcelColName(ByVal Col As Integer) As String
        If Col < 0 And Col > 256 Then
            MsgBox("Invalid Argument", MsgBoxStyle.Critical)
            Return Nothing
            Exit Function
        End If
        Dim i As Int16
        Dim r As Int16
        Dim S As String
        If Col <= 26 Then
            S = Chr(Col + 64)
        Else
            r = Col Mod 26
            i = System.Math.Floor(Col / 26)
            If r = 0 Then
                r = 26
                i = i - 1
            End If
            S = Chr(i + 64) & Chr(r + 64)
        End If
        ExcelColName = S
    End Function
    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        If SaveFileDialog1.ShowDialog = DialogResult.OK Then
            Dim sheetIndex As Integer
            Dim Ex As Object
            Dim Wb As Object
            Dim Ws As Object
            Ex = CreateObject("Excel.Application")
            Wb = Ex.workbooks.add
            Dim col, row As Integer
            Dim rawData(DataGridView1.Rows.Count, DataGridView1.Columns.Count - 1) As Object
            For col = 0 To DataGridView1.Columns.Count - 1
                rawData(0, col) = DataGridView1.Columns(col).HeaderText.ToUpper
            Next
            For col = 0 To DataGridView1.Columns.Count - 1
                For row = 0 To DataGridView1.Rows.Count - 1
                    rawData(row + 1, col) = DataGridView1.Rows(row).Cells(col).Value
                Next
            Next
            Dim finalColLetter As String = String.Empty
            finalColLetter = ExcelColName(DataGridView1.Columns.Count)
            sheetIndex += 1
            Ws = Wb.Worksheets(sheetIndex)
            Dim excelRange As String = String.Format("A1:{0}{1}", finalColLetter, DataGridView1.Rows.Count + 1)
            Ws.Range(excelRange, Type.Missing).Value2 = rawData
            Ws = Nothing
            Wb.SaveAs(SaveFileDialog1.FileName, Type.Missing, Type.Missing,
         Type.Missing, Type.Missing, Type.Missing, Type.Missing,
         Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)
            Wb.Close(True, Type.Missing, Type.Missing)
            Wb = Nothing
            ' Release the Application object
            Ex.Quit()
            Ex = Nothing
            ' Collect the unreferenced objects
            GC.Collect()
            MsgBox("Exported Successfully.", MsgBoxStyle.Information)
        End If
    End Sub
End Class



Imports System.IO

Public Class Form1

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'Save File In .txt (Notepad)
        Dim Save As New SaveFileDialog()
        Dim myStreamWriter As System.IO.StreamWriter
        Save.Filter = "Text [*.txt*]|*.txt"
        Save.CheckPathExists = True
        Save.Title = "Save File"
        Save.ShowDialog(Me)
        Try
            myStreamWriter = System.IO.File.AppendText(Save.FileName)
            myStreamWriter.Write(TextBox1.Text)
            myStreamWriter.Flush()
        Catch ex As Exception
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
         If SaveFileDialog1.ShowDialog = DialogResult.OK Then
            System.IO.File.WriteAllText(SaveFileDialog1.FileName, TextBox1.Text)
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SaveFileDialog1.Filter = "Word Documents|*.doc"
    End Sub
End Class

Post a Comment

  1. Je suis très ravis de ce site. Il est exceptionnel.

    ReplyDelete
  2. Thanks for sharing the code, work great!!!

    ReplyDelete
  3. Datagridview Data Export To Excel In Vb.Net With Save File Dialog >>>>> Download Now

    >>>>> Download Full

    Datagridview Data Export To Excel In Vb.Net With Save File Dialog >>>>> Download LINK

    >>>>> Download Now

    Datagridview Data Export To Excel In Vb.Net With Save File Dialog >>>>> Download Full

    >>>>> Download LINK oi

    ReplyDelete

 
Top