
20/03/2009, 09:45
|
 | | | Fecha de Ingreso: junio-2007 Ubicación: Shikasta
Mensajes: 1.272
Antigüedad: 17 años, 8 meses Puntos: 49 | |
Exportar a Excel contenido de DataGridView con los HeaderText Buenos dias compañeros, estoy exportando a excel el contenido de un datagridview a un excel utilizando el siguiente procedimiento:
Código:
'EXPORTAR A EXCEL CONTENIDO DATAGRIDVIEW
Public Sub ExportarExcel(ByVal Dg As DataGridView, ByVal Ruta As String)
On Error Resume Next
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(1)
For i = 0 To Dg.RowCount - 2
For j = 0 To Dg.ColumnCount - 1
xlWorkSheet.Cells(i + 1, j + 1) = Dg(j, i).Value.ToString()
Next
Next
If Ruta <> vbNullString Then
xlWorkSheet.SaveAs(Ruta)
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
MessageBox.Show("El Archivo fue exportado satisfactoriamente " + Ruta, "Exportación de Archivos", MessageBoxButtons.OK, MessageBoxIcon.Information)
Process.Start(Ruta)
Else
MessageBox.Show("Debe seleccionar la ubicación donde guardar el archivo", "Exportación de Archivos", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If
End Sub
'OBJETO DEL COMPONENTE COM
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
Eso hace el proceso sin ningún inconveniente, mi inquietud es como se haría para que tambien exportara los HeaderText de cada una de las columnas al excel.
Agradesco sus comentarios al respecto.
Saludos y Gracias.
__________________ "SELECT * FROM Mujeres WHERE situacion NOT IN ('CASADAS','CON HIJOS','ATORMENTADAS','CUASI-ENNOVIADAS') AND personalidad <> 'INTENSA'" |