Export Datagridview to Excel

Submitted by: 
Visitors have accessed this post 36288 times.

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 SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

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.

Comments

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

i am trying to import the data from excel into datagrid view and all those values should be updated in mysql...is it possible??

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()
Next
Next
Next

xlWorkSheet.SaveAs("D:\vbexcel.xlsx")
xlWorkBook.Close()
xlApp.Quit()

releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)

MsgBox("You can find the file D:\vbexcel.xlsx")
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

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

all steps to do login vb2008 with sql server database

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 dr.read
if dr.hasrows then
'allow user
else
'invalid username and password
end if
end while

Coolllllll..Thank u

instead of excel, can you use as ms access?

wat do u mean? export datagridview to ms access?
ahmmp is dot posible? hehehe

Tol ang ibig sabihin nya kung pwede gumamit ng access instead of excel nyahahaha

Web / System Developer
Contacts :
09327081978
[email protected] >>> Facebook/YM
ICCT Colleges Cainta Rizal

hahaha toink conflict naman kasi ung comment nya hahaha

Umm.....thanks for the sharing of code. I was looking for something like this.

dont mention it heheh dream it code it

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.