
20/03/2009, 12:47
|
 | | | Fecha de Ingreso: junio-2007 Ubicación: Shikasta
Mensajes: 1.272
Antigüedad: 17 años, 8 meses Puntos: 49 | |
Respuesta: Exportar a Excel contenido de DataGridView con los HeaderText Listo compañeros al fin lo pude solucionar de esta forma, recorriendo el Datatable:
Código:
'EXPORTAR A EXCEL CONTENIDO DATATABLE
Public Sub ExportToExcel(ByVal Dt As DataTable, ByVal Path As String)
Dim excelApplication As Excel.Application = New Excel.Application
Dim excelWorkbook As Excel.Workbook = CType(excelApplication.Workbooks.Add(System.Reflection.Missing.Value), Excel.Workbook)
Dim excelSheet As Excel.Worksheet = CType(excelWorkbook.Sheets(1), Excel.Worksheet)
For i As Integer = 0 To Dt.Columns.Count - 1
CType(excelSheet.Cells(1, i + 1), Excel.Range).Value2 = Dt.Columns(i).ColumnName
Next i
excelSheet.Range(excelSheet.Cells(1, 1), excelSheet.Cells(1, Dt.Columns.Count)).Font.Bold = True
excelSheet.Range(excelSheet.Cells(1, 1), excelSheet.Cells(1, Dt.Columns.Count)).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
For i As Integer = 0 To Dt.Rows.Count - 1
excelSheet.Cells.Range(excelSheet.Cells(i + 2, 1), excelSheet.Cells(i + 2, Dt.Columns.Count)).Value2 = Dt.Rows(i).ItemArray
Next i
Dim tmp As String
tmp = excelSheet.Cells.Range(excelSheet.Cells(2, 1), excelSheet.Cells(2, 1)).Value2.ToString()
If Path <> vbNullString Then
excelWorkbook.Close(True, Path, Nothing)
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWorkbook)
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApplication)
excelWorkbook = Nothing
excelApplication = Nothing
MessageBox.Show("El Archivo fue exportado satisfactoriamente " + Path, "Exportación de Archivos", MessageBoxButtons.OK, MessageBoxIcon.Information)
Process.Start(Path)
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
Saludos y Gracias por todo.
__________________ "SELECT * FROM Mujeres WHERE situacion NOT IN ('CASADAS','CON HIJOS','ATORMENTADAS','CUASI-ENNOVIADAS') AND personalidad <> 'INTENSA'" |