I have in my project 2 buttons to import from Excel and export to Excel and a datagridview. I can import Excel files with no problem but when I export from datagridview to Excel and try to re-import that file I only get just one column header with "F1" in it:
This is the code for export button:
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
'Creamos las variables
Dim exApp As New Excel.Application
Dim exLibro As Excel.Workbook
Dim exHoja As 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 = DataGridView1.ColumnCount
Dim NRow As Integer = DataGridView1.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) = DataGridView1.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) = DataGridView1.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.ToString)
End Try
End Sub
What can I do to solve it??
Thanks.


