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

Exportacion

The document describes a function that exports data from a DataGridView to an Excel spreadsheet. The function takes a DataGridView as a parameter and loops through its rows and columns to write the data to a new Excel workbook and worksheet. It formats the header row, sizes the columns automatically, and makes the Excel application visible before closing and returning true if successful.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Exportacion

The document describes a function that exports data from a DataGridView to an Excel spreadsheet. The function takes a DataGridView as a parameter and loops through its rows and columns to write the data to a new Excel workbook and worksheet. It formats the header row, sizes the columns automatically, and makes the Excel application visible before closing and returning true if successful.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Module Exportar

Function llenarExcel(ByVal ElGrid As DataGridView) As Boolean

'Creamos las variables

Dim exApp As New Microsoft.Office.Interop.Excel.Application

Dim exLibro As Microsoft.Office.Interop.Excel.Workbook

Dim exHoja As Microsoft.Office.Interop.Excel.Worksheet

Try

'A�adimos el Libro al programa, y la hoja al libro

exLibro = exApp.Workbooks.Add

exHoja = exLibro.Worksheets.Add()

' �Cuantas columnas y cuantas filas?

Dim NCol As Integer = ElGrid.ColumnCount

Dim NRow As Integer = ElGrid.RowCount

'Aqui recorremos todas las filas, y por cada fila todas las columnas

'y vamos escribiendo.

For i As Integer = 1 To NCol

exHoja.Cells.Item(1, i) = ElGrid.Columns(i - 1).Name.ToString

Next

For Fila As Integer = 0 To NRow - 1

For Col As Integer = 0 To NCol - 1

exHoja.Cells.Item(Fila + 2, Col + 1) = ElGrid.Item(Col,


Fila).Value

Next

Next

'Titulo en negrita, Alineado al centro y que el tama�o de la columna

'se ajuste al texto

exHoja.Rows.Item(1).Font.Bold = 1

exHoja.Rows.Item(1).HorizontalAlignment = 3

exHoja.Columns.AutoFit()

'Aplicaci�n visible
exApp.Application.Visible = True

exHoja = Nothing

exLibro = Nothing

exApp = Nothing

Catch ex As Exception

MsgBox(ex.Message, MsgBoxStyle.Critical, "Error al exportar a Excel")

Return False

End Try

Return True

End Function

End Module

You might also like