We will fully support you in this host. How we convert data grade view file to excel First, create a new project in Visual Studio and select the DataGradeView tool from there. Then grab a new button from the toolbox. Then use the code below. It converts the file in perfect condition. That means when the same table will be opened in Excel with everything that was written in Column and Rows. Nothing will be a data miss. This can be especially useful when we have to give the data of a database software or a medical store software to a customer in an Excel file. It is also used in many other functions. As any problem occurs in our database, we can immediately convert our data to Excel and keep it with us.
Step.1 paste or write the given coding in up from import then paste or write the save file dialog
Step.2 use the function of excel copy the code below then use it under the form
Step.3 it is the last option just use this coding for export button double click on button then use
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