Sebelumnya saya sudah pernah share kepada teman-teman cara mengexport data dari Listview ke Microsoft Excel. Bagi yang belum tau, silahkan klik link berikut untuk menuju ke T.K.P.. ^_^. Nah, kali ini saya akan share bagaimana cara export DataGridView ke Microsoft Excel. Untuk tidak memperpanjang mukaddimah (kayak ceramah ajha..), marilah kita mulai dengan membaca 'basmalah'..
Seperti biasa, kita harus Reference dahulu file berikut ini ke project kita :
- Microsoft.Office.Interop.Excel
- Microsoft office 12.0 object library
Setelah itu, tambahkan sebuah button yang nantinya kita isi coding untuk mengexport DataGridView ke Microsoft Excel.
Selanjutnya, tambahkan pada baris paling atas code editor :
Imports Excel = Microsoft.Office.Interop.Excel
Tambahkan script berikut pada button yang tadi :
Try
Dim xlApp As Microsoft.Office.Interop.Excel.Application
Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim i As Integer
Dim j As Integer
xlApp = New Microsoft.Office.Interop.Excel.Application
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
For i = 0 To DataGridView1.RowCount - 2
For j = 0 To DataGridView1.ColumnCount - 1
For k As Integer = 1 To DataGridView1.Columns.Count
xlWorkSheet.Cells(1, k) = DataGridView1.Columns(k - 1).HeaderText
xlWorkSheet.Cells(i + 2, j + 1) = DataGridView1(j, i).Value.ToString()
Next
Next
Next
xlWorkSheet.SaveAs("D:\vbexcel.xlsx")
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
MsgBox("Hasil export tersimpan di D:\vbexcel.xlsx")
Catch ex As Exception
End Try
End Sub
Private Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
Dim xlApp As Microsoft.Office.Interop.Excel.Application
Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim i As Integer
Dim j As Integer
xlApp = New Microsoft.Office.Interop.Excel.Application
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
For i = 0 To DataGridView1.RowCount - 2
For j = 0 To DataGridView1.ColumnCount - 1
For k As Integer = 1 To DataGridView1.Columns.Count
xlWorkSheet.Cells(1, k) = DataGridView1.Columns(k - 1).HeaderText
xlWorkSheet.Cells(i + 2, j + 1) = DataGridView1(j, i).Value.ToString()
Next
Next
Next
xlWorkSheet.SaveAs("D:\vbexcel.xlsx")
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
MsgBox("Hasil export tersimpan di D:\vbexcel.xlsx")
Catch ex As Exception
End Try
End Sub
Private Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
Berikut adalah Screenshoot nya :
Dan ini hasilnya :
Demikian tutorial singkat ini, semoga bermanfaat..!
thanks, sangat bermanfaat
BalasHapusjazakumulloh khoiron katsiro.. Barkallloh..
BalasHapusIf you enable all (*) DataGridView it's not work, but you so cool by share
BalasHapus