dear friends following is the code for export data from data grid to excel .. problem which i m facing that .. i could not export the last coloum values.. kindly give sulotion..

Inline Code Example Here Private Sub btnExcel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExcel.Click
Dim rowsTotal, colsTotal As Short
Dim I, j, iC As Short
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
Dim xlApp As New excel.Application

    Try
        Dim excelBook As excel.Workbook = xlApp.Workbooks.Add
        Dim excelWorksheet As excel.Worksheet = CType(excelBook.Worksheets(1), excel.Worksheet)
        xlApp.Visible = True
        rowsTotal = DataGridView1.RowCount - 1
        colsTotal = DataGridView1.Columns.Count - 1

        With excelWorksheet
            .Cells.Select()
            .Cells.Delete()
            For iC = 0 To colsTotal
                .Cells(1, iC + 1).Value = DataGridView1.Columns(iC).HeaderText
            Next

            For I = 0 To rowsTotal - 1
                For j = 0 To colsTotal - 1
                    .Cells(I + 2, j + 1).value = DataGridView1.Rows(I).Cells(j).Value
                Next j
            Next I

            .Rows("1:1").Font.FontStyle = "Bold"
            .Rows("1:1").Font.Size = 10
            .Cells.Columns.AutoFit()
            .Cells.Select()
            .Cells.EntireColumn.AutoFit()
            .Cells(1, 1).Select()
        End With

    Catch ex As Exception
        MsgBox("Export Excel Error " & ex.Message)
    Finally
        'RELEASE ALLOACTED RESOURCES
        System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
        xlApp = Nothing

    End Try

End Sub

End Class

I think your problem may lie with the rowsTotal -1.

Are you think of the rows as an array?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.