You can use this sample. Export from DataGridView to Excel
Imports Excel = Microsoft.Office.Interop.Excel
Dim excel As Excel.Application
Dim xWorkBook As Excel.Workbook
Dim xWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim row As Integer
Dim col As Integer
excel = New Excel.ApplicationClass
xWorkBook = excel.Workbooks.Add(misValue)
xWorkSheet = xWorkBook.Worksheets.Add
xWorkSheet.Name = "MyWorkSheet"
'Create your connection,sql statements etc. and fill in your DataGridview
'then follow this codes
For row = 0 To DataGridView1.Rows.Count - 2
For col = 0 To DataGridView1.Columns.Count - 1
xWorkSheet.Cells(8, col + 2) = DataGridView1.Columns.Item(col).HeaderText
xWorkSheet.Cells(row + 9, col + 2) = DataGridView1(col, row).Value.ToString()
'That's it. You can change all the variables to fit your project
'Save your Excel File
xWorkSheet.SaveAs("C:\Excel Exported Files\"&"MyExportedFile.xls")
'release all objects
Thanks ur reply. i have tried your code that you provided, unfortunately when i copy ur code into my coding page and run it, i get error "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." and stuck at "xWorkSheet.SaveAs("C:\abc.xls")"
I have tried also change the path name to server.mappath("abc.xls"), but still cannot. Can anyone tell me how i solve this problem?
i am facing problem when i need to create an excel file and insert some data inside. Can anyone help me? i am using vs 2005 and excel 2003.
Here the way to do it, and you have to explore by your self.
Dim _Ex = New Excel.Application
_Ex.DisplayAlerts = False ' Or True in case you want to see the alerts
Dim _Wb As Excel.Workbook
_Wb = _Ex.Workbooks.Add()
Dim _Ws As Excel.Worksheet
_Ws = _Wb.ActiveSheet
_Ex.Visible = True
' put some data
_Ex.ActiveCell.FormulaR1C1 = "My Data 1"
' or in single line such as
_Ws.Range("A2").FormulaR1C1 = "My Data 2"
' save the excel sheet
' quit the excel