0% found this document useful (0 votes)
23 views2 pages

Exporting a DataGridView to Excel in VB.NET

The document provides a VB.NET code snippet for exporting a DataGridView to an Excel file using Microsoft Office Interop libraries. It details the process of creating a new Excel workbook, copying column headers and data from the DataGridView, saving the file, and closing the application. Additionally, it notes the requirement to add references to the `Microsoft.Office.Interop.Excel` assembly in the VB.NET project.

Uploaded by

wilson green
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views2 pages

Exporting a DataGridView to Excel in VB.NET

The document provides a VB.NET code snippet for exporting a DataGridView to an Excel file using Microsoft Office Interop libraries. It details the process of creating a new Excel workbook, copying column headers and data from the DataGridView, saving the file, and closing the application. Additionally, it notes the requirement to add references to the `Microsoft.Office.Interop.Excel` assembly in the VB.NET project.

Uploaded by

wilson green
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

*Exporting a DataGridView to Excel in VB.

NET*

To export a DataGridView to an Excel file in VB.NET, you can use the Microsoft Office Interop libraries.
Here's an example code snippet:

```vb

Imports Microsoft.Office.Interop.Excel

Public Sub ExportDataGridViewToExcel(ByVal dgv As DataGridView)

Try

' Create a new Excel workbook

Dim excelApp As New Excel.Application()

Dim workbook As Excel.Workbook = excelApp.Workbooks.Add()

Dim worksheet As Excel.Worksheet = workbook.Sheets(1)

' Copy the column headers from the DataGridView to the Excel worksheet

For i As Integer = 0 To dgv.ColumnCount - 1

worksheet.Cells(1, i + 1) = dgv.Columns(i).HeaderText

Next

' Copy the data from the DataGridView to the Excel worksheet

For i As Integer = 0 To dgv.RowCount - 1

For j As Integer = 0 To dgv.ColumnCount - 1

worksheet.Cells(i + 2, j + 1) = dgv.Rows(i).Cells(j).Value

Next

Next
' Save the Excel file

Dim filePath As String = "C:\example.xlsx"

workbook.SaveAs(filePath)

' Close the Excel application

excelApp.Quit()

' Display a message to indicate the Excel file was created

MessageBox.Show("Excel file created: " & filePath)

Catch ex As Exception

MessageBox.Show("Error exporting to Excel: " & ex.Message)

End Try

End Sub

```

This code creates a new Excel workbook, copies the column headers and data from the DataGridView to
the Excel worksheet, saves the Excel file, and then closes the Excel application.

*Note:* To use the Microsoft Office Interop libraries, you'll need to add references to the
`Microsoft.Office.Interop.Excel` assembly in your VB.NET project.

_The answer does not rely on search results. Use the You.com app for the full experience
https://youcom.onelink.me/lbFr/whatsappEM_

_The answer does not rely on search results. Use the You.com app for the full experience
https://youcom.onelink.me/lbFr/whatsappEM_

You might also like