Skip to main content

Export Datagridview to Excel

Export Datagridview to Excel + Simple Database Query( Add, update, Delete, Search) Hope You Like It Guys :)

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.


Submitted byAnonymous (not verified)on Sun, 03/14/2010 - 13:57

Umm.....thanks for the sharing of code. I was looking for something like this.
Submitted bycedon Tue, 03/16/2010 - 17:52

In reply to by Anonymous (not verified)

dont mention it heheh dream it code it
Submitted bycidfreyon Tue, 03/16/2010 - 23:35

In reply to by ced

Tol ang ibig sabihin nya kung pwede gumamit ng access instead of excel nyahahaha
Submitted byAnonymous (not verified)on Fri, 03/26/2010 - 13:17

In reply to by cidfrey

hahaha toink conflict naman kasi ung comment nya hahaha
Submitted byAnonymous (not verified)on Wed, 07/21/2010 - 17:04

Coolllllll..Thank u

look for my work.. (login form) ???? try this.. hehe 'assume mo nalang cn = connection, dr = datareader, txtusername and txtpassword are textfield with cmd .connection = cn .commandtext = "Select * from tblLogin Where Username = ''' & txtUsername.text & "' and Password = '" & txtpassword.text & "' " end with dr = cmd.executeReader while if dr.hasrows then 'allow user else 'invalid username and password end if end while
Submitted byAnonymous (not verified)on Sun, 10/03/2010 - 18:01

Hello i was try to run this code but i got this error message: Error 6 Type 'Excel.Application' is not defined. C:\Users\Administrator\Desktop\Excel\WindowsApplication1\Form1.vb 51 22 WindowsApplication1
Submitted byAnonymous (not verified)on Fri, 12/31/2010 - 15:26

TRY THIS CODE ITS 100% working

Code below creates Excel File and saves it in D: drive
It uses Microsoft office 2007

FIRST ADD REFERRANCE (Microsoft office 12.0 object library ) to your project

Then Add code given bellow to the Export button click event-

Private Sub Export_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EXPORT_Button.Click

Dim xlApp As Microsoft.Office.Interop.Excel.Application
Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim i As Integer
Dim j As Integer

xlApp = New Microsoft.Office.Interop.Excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")

For i = 0 To DataGridView1.RowCount - 2
For j = 0 To DataGridView1.ColumnCount - 1
For k As Integer = 1 To DataGridView1.Columns.Count
xlWorkSheet.Cells(1, k) = DataGridView1.Columns(k - 1).HeaderText
xlWorkSheet.Cells(i + 2, j + 1) = DataGridView1(j, i).Value.ToString()



MsgBox("You can find the file D:\vbexcel.xlsx")
End Sub

Private Sub releaseObject(ByVal obj As Object)
obj = Nothing
Catch ex As Exception
obj = Nothing
End Try
End Sub

Submitted byuzumakicat (not verified)on Mon, 02/06/2012 - 14:36

thanks for this code. me project kasi ako...our client ask for excel. ... and i look for tutorials sa ibat ibang sites. thanks =D

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.