Imports System.IO Public Class cDgvToExcel Dim FlNm As String Private Sub ExportToExcel(ByVal DGV As DataGridView) Dim fs As New StreamWriter(FlNm, False) With fs .WriteLine("") .WriteLine("") .WriteLine("") .WriteLine(" ") .WriteLine(" ") .WriteLine(" ") .WriteLine(" ") .WriteLine(" ") ' If DGV.Name = "Student" Then .WriteLine(" ") 'SET NAMA SHEET .WriteLine(" ") .WriteLine(" ") 'No .WriteLine(" ") 'NIK .WriteLine(" ") 'Nama .WriteLine(" ") 'Alamat .WriteLine(" ") 'Telp ' End If 'AUTO SET HEADER .WriteLine(" ") For i As Integer = 0 To DGV.Columns.Count - 1 'SET HEADER Application.DoEvents() .WriteLine(" ") .WriteLine(" {0}", DGV.Columns.Item(i).HeaderText) .WriteLine(" ") Next .WriteLine(" ") For intRow As Integer = 0 To DGV.RowCount - 1 Application.DoEvents() .WriteLine(" ") For intCol As Integer = 0 To DGV.Columns.Count - 1 Application.DoEvents() .WriteLine(" ") .WriteLine(" {0}", DGV.Item(intCol, intRow).Value.ToString) .WriteLine(" ") Next .WriteLine(" ") Next .WriteLine("
") .WriteLine("
") .WriteLine("
") .Close() End With End Sub Public Sub start(DGV As DataGridView) If DGV.RowCount = 0 Then Return ' BtnExport.Text = "Please Wait..." ' BtnExport.Enabled = False Application.DoEvents() DGV.AllowUserToAddRows = False If False Then With DGV .AllowUserToAddRows = False .Name = "Student" .Visible = False .Columns.Clear() .Columns.Add("No", "No") .Columns.Add("NIK", "NIK") .Columns.Add("Nama", "Nama") .Columns.Add("Alamat", "Alamat") .Columns.Add("Telp", "Telp") End With With DGV If .Rows.Count > 0 Then For i As Integer = 0 To .Rows.Count - 1 Application.DoEvents() DGV.Rows.Add(IIf(i = 0, 1, i + 1), .Rows(i).Cells("NIK").Value, _ .Rows(i).Cells("Nama").Value, .Rows(i).Cells("Alamat").Value, _ .Rows(i).Cells("Telp").Value) Next End If End With End If FlNm = "D:\TEST" & Now.Day & "-" & Now.Month & "-" & Now.Year & ".xml" 'FlNm = Application.StartupPath & "\Student " _ ' & Now.Day & "-" & Now.Month & "-" & Now.Year & ".xls" If File.Exists(FlNm) Then File.Delete(FlNm) ExportToExcel(DGV) DGV.Dispose() DGV = Nothing Process.Start("EXCEL", "D:\TEST" & Now.Day & "-" & Now.Month & "-" & Now.Year & ".xml") ' BtnExport.Text = "Export" ' BtnExport.Enabled = True End Sub End Class