Dataset to Excel code probs

RaNdOmGuY

New member
Joined
Feb 28, 2006
Messages
2
Location
Australia, Victoria, East Coast
Programming Experience
Beginner
Hi, i'm trying to export a datagrid to excel with the code below... Now i've kind of jumped into the middle without actually fully understanding some common errors...Below highlighted in red are the problem lines, i am recieving "Type <> is not defined" and "Name <> is not declared" i'm not quite sure, but would i be right in saying that i haven't imported something?? i'm using vs.net 2003 if that helps....any help is much appreciated, thanks in advance

VB.NET:
    Private Sub ExportGridToExcel()

        Dim Excel As Object = CreateObject("Excel.Application")

        If Excel Is Nothing Then
            MsgBox("It appears that Excel is not installed on this machine. This operation requires MS Excel to be installed on this machine.", MsgBoxStyle.Critical)
            Return
        End If

        'Make Excel visible
        Excel.Visible = True

        'Initialize Excel Sheet
        With Excel
            .SheetsInNewWorkbook = 1
            .Workbooks.Add()
            .Worksheets(1).Select()


            'Add header row to Excel Sheet by copying column headers from the Datagrid 
            Dim Col As [B][COLOR="Red"]DataGridViewColumn[/COLOR][/B]
            Dim i As Integer = 1
            For Each Col In [B][COLOR="red"]dgRemovaLog[/COLOR][/B].Columns
                .Cells(1, i).Value = Col.HeaderText
                i += 1
            Next

            'Add data to excel sheet by looping through the rows
            'in the datagrid
            i = 2
            Dim RowItem As [B][COLOR="red"]DataGridViewRow[/COLOR][/B]
            Dim Cell As [B][COLOR="red"]DataGridViewCell[/COLOR][/B]
            For Each RowItem In [B][COLOR="red"]dgRemovaLog[/COLOR][/B].Rows
                Dim j As Integer = 1
                For Each Cell In RowItem.Cells
                    .Cells(i, j).Value = Cell.Value
                    j += 1
                Next
                i += 1
            Next
        End With

        Excel.Quit()
        System.Runtime.InteropServices.Marshal.ReleaseComObject(Excel)
        Excel = Nothing
        MsgBox("Export to Excel Complete", MsgBoxStyle.Information)
    End Sub
 
Last edited:
Back
Top