I am able to save the datagridview as an excel sheet but everytime i do it,the previously saved file gets replaced which i do not want..i want the file to be saved with a new name everytime
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim i As Integer
Dim j As Integer
xlApp = New 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
xlWorkSheet.Cells(i + 1, j + 1) = _
DataGridView1(j, i).Value.ToString()
Next
Next
For j = 0 To DataGridView1.ColumnCount - 1
xlWorkSheet.Cells(1, j + 1) = DataGridView1.Columns(j).Name
Next
For i = 0 To DataGridView1.RowCount - 1
For j = 0 To DataGridView1.ColumnCount - 1
Dim cell As DataGridViewCell
cell = DataGridView1(j, i)
xlWorkSheet.Cells(i + 2, j + 1) = cell.Value
Next
Next
xlWorkSheet.SaveAs("C:\Users\Space Era\Documents\vbexcel.xlsx")
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
MsgBox("You can find the file C:\Users\Space Era\Documents\vbexcel.xlsx")
End Sub