Click here to Skip to main content
11,436,803 members (61,361 online)
Click here to Skip to main content
Add your own
alternative version

Export DataGridView to Excel

, 22 Nov 2012 CPOL
How to export a DataGridView to Excel.
ExcelExporterUpdated.zip
Option Explicit On
Option Strict On
Imports x = Microsoft.Office.Interop.Excel

Friend Class ExcelExporter
    Friend Function Export(ByRef dgv As DataGridView, ByVal Path As String) As Boolean
        Export = False
        Try
            Dim xlApp As x.Application, xlWorkBook As x.Workbook, xlWorkSheet As x.Worksheet
            Dim MissingValue As Object = System.Reflection.Missing.Value

            xlApp = New x.Application
            xlWorkBook = xlApp.Workbooks.Add(MissingValue)
            xlWorkSheet = CType(xlWorkBook.Sheets.Item(1), Microsoft.Office.Interop.Excel.Worksheet)

            'get all visible solumns in display index order
            Dim ColNames As List(Of String) = (From col As DataGridViewColumn In dgv.Columns.Cast(Of DataGridViewColumn)() _
                                               Where col.Visible = True _
                                               Order By col.DisplayIndex _
                                               Select col.Name).ToList

            'get headers
            Dim colcount = 0
            For Each s In ColNames
                colcount += 1
                xlWorkSheet.Cells(1, colcount) = dgv.Columns.Item(s).HeaderText
            Next

            'get values
            For rowcount = 0 To dgv.Rows.Count - 1  'for each row
                colcount = 0
                For Each s In ColNames 'for each column
                    colcount += 1
                    xlWorkSheet.Cells(rowcount + 2, colcount) = dgv.Rows(rowcount).Cells(s).Value
                    'xlWorkSheet.Cells(rowcount + 2, colcount) = dgv.Rows(rowcount).Cells(s).FormattedValue
                Next s
            Next

            xlWorkSheet.SaveAs(Path)
            xlWorkBook.Close()
            xlApp.Quit()
            ReleaseObject(xlWorkSheet)
            ReleaseObject(xlWorkBook)
            ReleaseObject(xlApp)
            Return True
        Catch ex As Exception
        End Try
    End Function

    Shared Sub ReleaseObject(ByVal o As Object)
        Try
            Runtime.InteropServices.Marshal.ReleaseComObject(o)
            o = Nothing
        Catch ex As Exception
            o = Nothing
        Finally
#If DEBUG Then
            GC.Collect()
#End If
        End Try
    End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

About the Author

Michael Rosqvist
Web Developer
Sweden Sweden
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150428.2 | Last Updated 22 Nov 2012
Article Copyright 2012 by Michael Rosqvist
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid