0

tengo un inconveniente al exportar los datos de un datagrid a excel el inconveniente es que siempre me salta el primer registro es decir no lo exporta si son 85 registros me exporta 84 y no se porque el bucle esta fallando en esa parte.

aqui mi buque que exporta

 Dim columnsCount As Integer = DataGridView1.Columns.Count
            For i As Integer = 0 To DataGridView1.Rows.Count - 1
                For j As Integer = 0 To DataGridView1.Columns.Count - 1
                    If i = 0 Then 'pintamos cabecera
                        hoja_trabajo.Cells(i + 1, j + 1) = DataGridView1.Columns(j).HeaderText

                    Else 'pintamos datos

                        hoja_trabajo.Cells(i + 1, j + 1) = DataGridView1.Rows(i).Cells(j).Value.ToString()


                    End If
                Next
            Next

1 respuesta 1

0

No estoy seguro pero ciando hacer If i = 0 Then te estas saltando el primer registro y por eso te sale una linea menos.

        For i As Integer = 0 To DataGridView1.Rows.Count - 1
            For j As Integer = 0 To DataGridView1.Columns.Count - 1
                If i = 0 Then 'pintamos cabecera
                    hoja_trabajo.Cells(1, j + 1) = DataGridView1.Columns(j).HeaderText
                 End If

                    hoja_trabajo.Cells(2 + i, j + 1) = DataGridView1.Rows(i).Cells(j).Value.ToString()

            Next
        Next

O también

      For i As Integer = 0 To DataGridView1.Rows.Count
            For j As Integer = 0 To DataGridView1.Columns.Count - 1
                If i = 0 Then 'pintamos cabecera
                    hoja_trabajo.Cells(i + 1, j + 1) = DataGridView1.Columns(j).HeaderText

                Else 'pintamos datos

                    hoja_trabajo.Cells(i + 1, j + 1) = DataGridView1.Rows(i -1).Cells(j).Value.ToString()


                End If
            Next
        Next
3
  • remplazando esa linea ahora me quita el ultimo registro
    – juan perez
    el 25 feb. 2020 a las 2:40
  • No estoy seguro si funcione la actualización que puse, perdón pero no tengo forma de probarlo :(
    – Jorge Luis
    el 25 feb. 2020 a las 2:48
  • al parecer el segundo ejemplo si funciono compañero muchas gracias por su colaboración.
    – juan perez
    el 25 feb. 2020 a las 14:31

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.