How to export from DataGridView to excel using VB.net

By Issac Viewed: 31860 times Emailed: 146 times Printed: 182 times Bookmark and Share



For exporting data from Datagridview to Excel, connect the database and load data from the database to Datagridview and then create a new excel file and write the data from Datagridview to Excel file.

  vb.net_export_datagridview_toexcel.GIF

First step is to Load the Product table data to DataGridView, and create new Excel file and write the data from Datagridview to Excel file.

Imports System.Data
Imports System.Data.SqlClient
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click

        Dim cnn As SqlConnection
        Dim connectionString As String
        Dim sql As String

        connectionString = "data source=servername;" & _
        "initial catalog=databasename;user id=username;password=password;"
        cnn = New SqlConnection(connectionString)
        cnn.Open()
        sql = "SELECT * FROM Product"
        Dim dscmd As New SqlDataAdapter(sql, cnn)
        Dim ds As New DataSet
        dscmd.Fill(ds)
        DataGridView1.DataSource = ds.Tables(0)
        cnn.Close()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button2.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.ApplicationClass
        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

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

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

        MsgBox("You can find the file C:\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
End Class




Comments(2)


1. It's very helpful to me... Thanks...

By: Nicks Patel at 2010-03-24 23:37:04
2. Hello,

Is there anyway the datagridview can export the column name too?

Douglas

By: Douglas at 2010-05-13 22:10:09

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:


Enter Code:
The Captcha image

Latest Tutorials

[2009-03-22]Creating List Views in Code using VB.net
[2009-03-22]Creating Tree Views in Code using VB.net
[2009-03-22]Creating Context Menus in Code using VB.net
[2009-03-22]Creating Menus in Code using VB.net
[2009-03-22]Handling Timer Events - and Creating an Alarm Clock in VB.net
[2009-03-16]Send SMS using VB code
[2009-02-27]Creating a Windows Service Installer in VB.net
[2009-02-27]Creating a Windows Service in VB.net
[2009-02-24]DataRow Class in VB.net
[2009-02-24]DataTable Class in VB.net
[2009-02-24]DataSet Class in VB.net
[2009-02-24]OleDbDataAdapter class in VB.net
[2009-02-24]OleDbConnection class in VB.net
[2009-02-22]A tutorial on Chat Server and Chat Client in VB.net
[2009-02-22]Chat Server in VB.net

More Latest News

Most Viewed Articles (in last 30 days)
For Loop in VB.net
How to export from DataGridView to excel using VB.net
Your first VB.NET Crystal Reports - A step by step guide
Send SMS using VB code
Using Select Case in VB.net
Arrays and Dynamic Arrays in VB.net
The Select Case statement in VB.net
While Loop in VB.net
How to send email using VB.NET code
If…Else Statements in VB.net
Insert cell data in an Excel file using OLEDB in VB.net
Sub Procedures and Functions in VB.net
Read Data from Excel using OLEDB in VB.NET 2005
Client Socket Program sample in VB.net
Chat Server in VB.net
Most Emailed Articles (in last 30 days)
For Loop in VB.net
If…Else Statements in VB.net
Send SMS using VB code
While Loop in VB.net
The Option and Imports Statements in VB .NET
What's New in VB .NET? A comparison of VB vs VB.net
For Each…Next Loop in VB.net
Visual Basic Statements
Using Select Case in VB.net
The Select Case statement in VB.net
Do Loop in VB.net
“Using If with And” and Comparing two integers using If
Data types in VB.net
What is .NET Framework and the Common Language Runtime?
Arrays and Dynamic Arrays in VB.net