Click here to Skip to main content
11,629,614 members (76,754 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
Hi,


How To export DataGridview Value to Excel in VB.NET 2.0 Windows Form
Posted 13-Jan-12 0:54am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,

 Private Sub btn_excel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_excel.Click
        'Export DataGridView to Excel
        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 gv.RowCount - 2
            For j = 0 To gv.ColumnCount - 1
                xlworksheet.Cells(i + 1, j + 1) = _
                    gv(j, i).Value.ToString()
            Next
        Next
        Dim dialog As New SaveFileDialog
        Dim result As DialogResult = dialog.ShowDialog
        Try
            xlworksheet.SaveAs(dialog.FileName)
        Catch exerr As Exception
        End Try
 
        xlworkbook.Close()
        xlapp.Quit()
 
        releaseObject(xlapp)
        releaseObject(xlworkbook)
        releaseObject(xlworksheet)
 
    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
  Permalink  
v2
Comments
mitchiee1226 at 8-Jul-14 4:35am
   
How do you put the Title of the columns in this code?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Look at this excel api[^], on codeplex, it supports .NET Framework 2.0 and above, please see below code:
Private Sub button1_Click(sender As Object, e As EventArgs)
    'connect with database
    Dim connection As New OleDbConnection()
    connection.ConnectionString = "Provider=""Microsoft.Jet.OLEDB.4.0"";Data Source=""demo.mdb"";User Id=;Password="
    Dim command As New OleDbCommand()
    command.CommandText = "select * from parts"
    Dim dataSet As DataSet = New System.Data.DataSet()
    Dim dataAdapter As New OleDbDataAdapter(command.CommandText, connection)
    dataAdapter.Fill(dataSet)
    Dim dt As DataTable = dataSet.Tables(0)
    Me.dataGridView1.DataSource = dt
    'export data to Excel
    Dim book As New Workbook()
    Dim sheet As Worksheet = book.Worksheets(0)
    book.Worksheets(0).InsertDataTable(TryCast(Me.dataGridView1.DataSource, DataTable), True, 1, 1)
    book.SaveToFile("sample.xlsx", ExcelVersion.Version2010)
    System.Diagnostics.Process.Start("sample.xlsx")
End Sub
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


Advertise | Privacy | Mobile
Web03 | 2.8.150723.1 | Last Updated 31 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100