How to export excel file from datagridview by having the user choose it's destination file and it's filename?
What I have tried:
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim i As Int16, j As Int16
Dim xlApp As Excel.Application = New Microsoft.Office.Interop.Excel.Application()
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
xlWorkSheet.Cells(1, 1) = "DATE HANDLED"
xlWorkSheet.Cells(1, 2) = "ROUTE"
xlWorkSheet.Cells(1, 3) = "ITIN NO"
xlWorkSheet.Cells(1, 4) = "METER NUMBER"
xlWorkSheet.Cells(1, 5) = "L.I.N"
xlWorkSheet.Cells(1, 6) = "ADDRESS"
xlWorkSheet.Cells(1, 7) = "FF DESCRIPTION AND REMARKS"
xlWorkSheet.Cells(1, 8) = "RDG"
For i = 0 To dt_data.RowCount - 2
For j = 0 To dt_data.ColumnCount - 1
xlWorkSheet.Cells(i + 3, j + 1) = dt_data(j, i).Value.ToString()
Next
Next
xlWorkBook.SaveAs("f:\vb.net-informations.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue,
Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue)
xlWorkBook.Close(True, misValue, misValue)
xlApp.Quit()
releaseObject(xlWorkSheet)
releaseObject(xlWorkBook)
releaseObject(xlApp)
MessageBox.Show("Saved")
and here is the releaseObject class
Private Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
MessageBox.Show("Exception Occured while releasing object " + ex.ToString())
Finally
GC.Collect()
End Try
End Sub