Hi every one,
I need help urgently please
I am creating a db with an interface from VB.Net 2008, what I need is to export to excel from a datagrideview by clicking on a button.
I have found a code but its giving me errors, I posted the code maybe u can find the error or point me to another method

Imports Excel = Microsoft.Office.Interop.Excel
Imports System.Data
Imports System.Data.SqlClient
Imports System.Windows.Forms

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim RowsCount As Int32 = Me.dgvCompany.SelectedRows.Count - 1

        If RowsCount > -1 Then
            'The array for field names.
            Dim FldNames() As String = {"Company_name", "", "City", "Region", "Country"}
            'The array for the selected records.
            Dim DataArr(RowsCount, 4) As Object
            Dim ColsCounter As Int32 = 0

            'Populate the data array - The list is sorted in ascending order.
            For RowsCounter As Int32 = 0 To RowsCount
                For Each cell As DataGridViewCell In Me.dgvCompany _
                                                     .SelectedRows(RowsCount - RowsCounter) _
                                                     .Cells
                    DataArr(RowsCounter, ColsCounter) = cell.Value
                    ColsCounter = ColsCounter + 1
                Next
                ColsCounter = 0
            Next

            'Populate the data array - The list is sorted in descending order. 
            'Dim Rows As Int32 = 0
            'For Each row As DataGridViewRow In Me.DataGridView1.SelectedRows
            'For Each cell As DataGridViewCell In Me.DataGridView1.SelectedRows(Rows).Cells
            'DataArr(Rows, ColsCounter) = cell.Value
            'ColsCounter = ColsCounter + 1
            'Next
            'ColsCounter = 0
            'Rows = Rows + 1
            'Next

            'Variables for Excel.
            Dim xlApp As New Excel.Application
            Dim xlWBook As Excel.Workbook = xlApp.Workbooks.Add( _
                                            Excel.XlWBATemplate.xlWBATWorksheet)
            Dim xlWSheet As Excel.Worksheet = CType(xlWBook.Worksheets(1), Excel.Worksheet)
            Dim xlCalc As Excel.XlCalculation

            'Save the present setting for Excel's calculation mode and turn it off. 
            With xlApp
                xlCalc = .Calculation
                .Calculation = Excel.XlCalculation.xlCalculationManual
            End With

            'Write the field names and the data to the targeting worksheet.
            With xlWSheet
                .Range(.Cells(1, 1), .Cells(1, 5)).Value = FldNames
                .Range(.Cells(2, 1), .Cells(RowsCount + 2, 5)).Value = DataArr
                .UsedRange.Columns.AutoFit()
            End With

            With xlApp
                .Visible = True
                .UserControl = True
                'Restore the calculation mode. 
                .Calculation = xlCalc
            End With

            'Relase objects from memory.                         
            xlWSheet = Nothing
            xlWBook = Nothing
            xlApp = Nothing
            GC.Collect()
        End If


    End Sub
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.