Private Sub ExportExcel() Dim xlApp As Microsoft.Office.Interop.Excel.Application Dim xlBook As Microsoft.Office.Interop.Excel.Workbook Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet Dim oValue As Object = System.Reflection.Missing.Value Dim sPath As String = String.Empty Dim dlgSave As New SaveFileDialog dlgSave.DefaultExt = "xls" dlgSave.Filter = "Microsoft Excel|*.xls" dlgSave.InitialDirectory = Application.StartupPath If dlgSave.ShowDialog = Windows.Forms.DialogResult.OK Then Try xlApp = New Microsoft.Office.Interop.Excel.Application xlBook = xlApp.Workbooks.Add(oValue) xlSheet = xlBook.Worksheets("sheet1") Dim xlRow As Long = 2 Dim xlCol As Short = 1 For k As Integer = 0 To DataGridView1.ColumnCount - 1 xlSheet.Cells(1, xlCol) = DataGridView1(k, 0).Value xlCol += 1 Next For i As Integer = 0 To DataGridView1.RowCount - 1 xlCol = 1 For k As Integer = 0 To DataGridView1.ColumnCount - 1 xlSheet.Cells(1, k + 1).Value = DataGridView1.Columns(k).HeaderText xlSheet.Cells(xlRow, xlCol) = DataGridView1(k, i).Value xlCol += 1 Next xlRow += 1 Next xlSheet.Columns.AutoFit() Dim sFileName As String = Replace(dlgSave.FileName, ".xls", "xls") xlSheet.SaveAs(sFileName) xlBook.Close() xlApp.Quit() releaseObject(xlApp) releaseObject(xlBook) releaseObject(xlSheet) MsgBox("Data sudah berhasil di ekspor", MsgBoxStyle.Information, "PRMS/SOB Date Tagging") Catch MsgBox(ErrorToString) Finally End Try End If 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
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)